めも。備忘録。
■実装
以下のようにして実装する。
package info.justoneplanet.android.sample.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
public static final int NOTIFICATION_REQUEST_CODE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setNotification();
}
/**
* 通知のセットアップ
*/
private void setNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,// 表示するアイコン
getString(R.string.hello),// ステータスバーに表示するテキスト
System.currentTimeMillis()// 表示する時間
);
// ペンディングできるIntentの入れ物
PendingIntent intent = PendingIntent.getActivity(
this,
NOTIFICATION_REQUEST_CODE,
new Intent(this, NotificationActivity.class),
Intent.FLAG_ACTIVITY_NEW_TASK
);
// お知らせをクリックした時に起動するActivityを指定する
notification.setLatestEventInfo(
this,
getString(R.string.app_name2),// 通知を開いた時のタイトル
getString(R.string.hello2),// 通知を開いた時の本文
intent
);
notification.vibrate = new long[]{0, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200};
notificationManager.notify(R.string.app_name, notification);// 通知のID
}
}
このように実装する事はまずないだろう。
Alarmと組み合わせる
MainActivity.java
PendingIntent.getBroadcastで生生したBroadcastReceicerをAlarmManagerに結びつける。
package info.justoneplanet.android.sample.notification;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
public static final int NOTIFICATION_BROADCAST_REQUEST_CODE = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
new Date().getTime() + 1000,// 1秒後
AlarmManager.INTERVAL_DAY,// 1日毎
PendingIntent.getBroadcast(
this,
NOTIFICATION_BROADCAST_REQUEST_CODE,
new Intent(this, NotificationReceiver.class),
Intent.FLAG_ACTIVITY_NEW_TASK
)
);
}
}
NotificationReceiver.java
broadcast receiver。contextが引き数で受け取れるのでActivityの時と同じ使い勝手でnotificationをセットアップする事ができる。
package info.justoneplanet.android.sample.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationReceiver extends BroadcastReceiver {
@SuppressWarnings("unused")
private static final String TAG = NotificationReceiver.class.getSimpleName();
@SuppressWarnings("unused")
private final NotificationReceiver self = this;
public static final int NOTIFICATION_REQUEST_CODE = 100;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,// 表示するアイコン
context.getString(R.string.hello),// ステータスバーに表示するテキスト
System.currentTimeMillis()// 表示する時間
);
// ペンディングできるIntentの入れ物
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
NOTIFICATION_REQUEST_CODE,
new Intent(context, NotificationActivity.class),
0
);
// お知らせをクリックした時に起動するActivityを指定する
notification.setLatestEventInfo(
context,
context.getString(R.string.app_name2),// 通知を開いた時のタイトル
context.getString(R.string.hello2),// 通知を開いた時の本文
pendingIntent
);
notification.vibrate = new long[]{0, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200};
notificationManager.notify(R.string.app_name, notification);
}
}
manifest
以下のように正しくreceiverを設定しないと上手く動作しない。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.justoneplanet.android.sample.notification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver
android:name=".NotificationReceiver"
android:process=":remote" />
<activity
android:label="@string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".NotificationActivity" >
</activity>
</application>
</manifest>
ServiceもBroadcastReceiverと組み合わせてnotificationを出せるし、C2DMもBroadcastReceiverなので同様である。
notificationの削除
起動されたActivity側で以下のようにして起動元別にキャンセル処理を挟む必要がある。
Intent intent = getIntent();
String origin = intent.getStringExtra("origin");
if (origin != null && origin.equals("notification")) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(R.string.app_name);
}