结合代码解释:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
...
private void showNotification(Message msg, int id) {
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notiicon, msg
.getTitle(), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", msg.getInfo());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
contentIntent);
notiManager.notify(id, notification);
1. 从系统中获得Notification服务,getSystemService()
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. 构造一个Notification,包括图标,图标的文字,以及Notification时间部分,通常使用当前时间。
FLAG_AUTO_CANCEL说明Notification点击一次就消失。
Notification notification = new Notification(R.drawable.notiicon, msg
.getTitle(), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
3.构造一个Intent, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK 两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", msg.getInfo());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
4. PendingIntent 是Intent的包装和描述。 把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。
PendingIntent contentIntent = PendingIntent.getActivity(this, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
5. 发送Notification。
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
contentIntent);
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
...
private void showNotification(Message msg, int id) {
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notiicon, msg
.getTitle(), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", msg.getInfo());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
contentIntent);
notiManager.notify(id, notification);
}
还可以通过设置资源文件来定义显示内容等:
String strText = getResources().getString(R.string.network_switch_notify_title);
String strNotice = getResources().getString(R.string.for_user_guide);
1. 从系统中获得Notification服务,getSystemService()
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2. 构造一个Notification,包括图标,图标的文字,以及Notification时间部分,通常使用当前时间。
FLAG_AUTO_CANCEL说明Notification点击一次就消失。
Notification notification = new Notification(R.drawable.notiicon, msg
.getTitle(), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
3.构造一个Intent, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK 两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", msg.getInfo());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
4. PendingIntent 是Intent的包装和描述。 把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。
PendingIntent contentIntent = PendingIntent.getActivity(this, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
5. 发送Notification。
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),
contentIntent);
notiManager.notify(id, notification);
Android tethering 实例
Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
availableList);
broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
erroredList);
mContext.sendStickyBroadcast(broadcast);
if (DBG) Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
activeList.size() + ", " + erroredList.size());
if (usbTethered) {
if (wifiTethered) {
showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
} else {
showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
}
} else if (wifiTethered) {
showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
} else {
clearTetheredNotification();
}
}
private void showTetheredNotification(int icon) {
NotificationManager notificationManager =
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
if (mTetheredNotification != null) {
if (mTetheredNotification.icon == icon) {
return;
}
notificationManager.cancel(mTetheredNotification.icon);
}
Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
Resources r = Resources.getSystem();
CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
CharSequence message = r.getText(com.android.internal.R.string.
tethered_notification_message);
if(mTetheredNotification == null) {
mTetheredNotification = new Notification();
mTetheredNotification.when = 0;
}
mTetheredNotification.icon = icon;
mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
mTetheredNotification.tickerText = title;
mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
}