下面列出了android.app.NotificationManager#notifyAsUser ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateAlwaysOnNotification(DetailedState networkState) {
final boolean visible = (mAlwaysOn && networkState != DetailedState.CONNECTED);
final UserHandle user = UserHandle.of(mUserHandle);
final long token = Binder.clearCallingIdentity();
try {
final NotificationManager notificationManager = NotificationManager.from(mContext);
if (!visible) {
notificationManager.cancelAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED, user);
return;
}
final Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString(mContext.getString(
R.string.config_customVpnAlwaysOnDisconnectedDialogComponent)));
intent.putExtra("lockdown", mLockdown);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent configIntent = mSystemServices.pendingIntentGetActivityAsUser(
intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT, user);
final Notification.Builder builder =
new Notification.Builder(mContext, SystemNotificationChannels.VPN)
.setSmallIcon(R.drawable.vpn_connected)
.setContentTitle(mContext.getString(R.string.vpn_lockdown_disconnected))
.setContentText(mContext.getString(R.string.vpn_lockdown_config))
.setContentIntent(configIntent)
.setCategory(Notification.CATEGORY_SYSTEM)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setColor(mContext.getColor(R.color.system_notification_accent_color));
notificationManager.notifyAsUser(TAG, SystemMessage.NOTE_VPN_DISCONNECTED,
builder.build(), user);
} finally {
Binder.restoreCallingIdentity(token);
}
}
@VisibleForTesting
protected void showTetheredNotification(int id, boolean tetheringOn) {
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
int icon = 0;
switch(id) {
case SystemMessage.NOTE_TETHER_USB:
icon = com.android.internal.R.drawable.stat_sys_tether_usb;
break;
case SystemMessage.NOTE_TETHER_BLUETOOTH:
icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth;
break;
case SystemMessage.NOTE_TETHER_GENERAL:
default:
icon = com.android.internal.R.drawable.stat_sys_tether_general;
break;
}
if (mLastNotificationId != 0) {
if (mLastNotificationId == icon) {
return;
}
notificationManager.cancelAsUser(null, mLastNotificationId,
UserHandle.ALL);
mLastNotificationId = 0;
}
Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
null, UserHandle.CURRENT);
Resources r = Resources.getSystem();
final CharSequence title;
final CharSequence message;
if (tetheringOn) {
title = r.getText(com.android.internal.R.string.tethered_notification_title);
message = r.getText(com.android.internal.R.string.tethered_notification_message);
} else {
title = r.getText(com.android.internal.R.string.disable_tether_notification_title);
message = r.getText(com.android.internal.R.string.disable_tether_notification_message);
}
if (mTetheredNotificationBuilder == null) {
mTetheredNotificationBuilder =
new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS);
mTetheredNotificationBuilder.setWhen(0)
.setOngoing(true)
.setColor(mContext.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setCategory(Notification.CATEGORY_STATUS);
}
mTetheredNotificationBuilder.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pi);
mLastNotificationId = id;
notificationManager.notifyAsUser(null, mLastNotificationId,
mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL);
}
@Override
public void handleMessage(Message msg) {
final Context context = mService.mContext;
final NotificationManager notifManager = context
.getSystemService(NotificationManager.class);
final int max = msg.arg1;
final int index = msg.arg2;
switch (msg.what) {
case MSG_SHOW:
final CharSequence title = context
.getText(R.string.android_upgrading_notification_title);
final Intent intent = new Intent();
intent.setClassName("com.android.settings",
"com.android.settings.HelpTrampoline");
intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");
final PendingIntent contentIntent;
if (context.getPackageManager().resolveActivity(intent, 0) != null) {
contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
} else {
contentIntent = null;
}
final Notification notif =
new Notification.Builder(mService.mContext,
SystemNotificationChannels.UPDATES)
.setSmallIcon(R.drawable.stat_sys_adb)
.setWhen(0)
.setOngoing(true)
.setTicker(title)
.setColor(context.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setContentTitle(title)
.setContentIntent(contentIntent)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setProgress(max, index, false)
.build();
notifManager.notifyAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING, notif,
UserHandle.of(mUserId));
break;
case MSG_HIDE:
notifManager.cancelAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING,
UserHandle.of(mUserId));
break;
}
}