下面列出了android.app.NotificationManager#cancel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean deleteDownloaded(final DownloadInfo info) {
Logger.d(TAG, "deleteDownloaded() :" + info.title);
// info.setState(DownloadInfo.STATE_CANCEL);
downloadedData.remove(info.videoid);
if (YoukuPlayerConfiguration.getPreference(KEY_LAST_NOTIFY_TASKID).equals(info.taskId)) {
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFY_ID);
YoukuPlayerConfiguration.savePreference(KEY_LAST_NOTIFY_TASKID, "");
}
new Thread() {
public void run() {
PlayerUtil.deleteFile(new File(info.savePath));
};
}.start();
startNewTask();
return true;
}
public static void RiseDropAlert(Context context, boolean on, String type, String message, int notificatioId) {
if(on) {
// This alerts will only happen once. Want to have maxint, but not create overflow.
OtherAlert(context, type, message, notificatioId, Integer.MAX_VALUE / 100000);
} else {
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(notificatioId);
UserNotification.DeleteNotificationByType(type);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public synchronized void onEventMainThread(HomeAction action) {
Object[] objects = null;
if (action.getObject() != null) {
objects = (Object[]) action.getObject();
}
switch (action.getType()) {
case DELAY_EXIT://Timeout logged out
mHandler.sendEmptyMessageDelayed(TIMEOUT_DELAYEXIT, 1000);
break;
case EXIT:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;
mNotificationManager.cancel(1001);
mHandler.removeMessages(TIMEOUT_DELAYEXIT);
//Remove the local login information
SharedPreferenceUtil.getInstance().remove(SharedPreferenceUtil.USER_INFO);
//close socket
MemoryDataManager.getInstance().clearMap();
PushMessage.pushMessage(ServiceAck.EXIT_ACCOUNT, new byte[0],ByteBuffer.allocate(0));
SharePreferenceUser.unLinkSharePreferrnce();
DaoManager.getInstance().closeDataBase();
HttpsService.stopServer(activity);
ProgressUtil.getInstance().dismissProgress();
ActivityUtil.next(activity, LoginForPhoneActivity.class);
finish();
break;
case TOCHAT:
ChatActivity.startActivity(activity, (Talker) (objects[0]));
break;
}
}
@Override
public void onCacheWordOpened()
{
Log.i(TAG, "SplashScreenActivity.onCacheWordOpened() called.");
// Clear any 'unlock' notifications
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NotificationsService.getUnlockNotificationId());
// If the extra to delay opening the main app has been set, wait for a couple of seconds
if (this.getIntent().hasExtra(EXTRA_DELAY_APP_OPENING))
{
Log.i(TAG, "Delaying the opening of the inbox by " + DELAY_OPENING_PERIOD_SECONDS + " seconds");
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
openInboxActivity();
}
}, DELAY_OPENING_PERIOD_SECONDS * 1000);
}
else
{
openInboxActivity();
}
}
static void removeProfileNotification(Context context)
{
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.cancel(
PPApplication.GRANT_PROFILE_PERMISSIONS_NOTIFICATION_TAG,
PPApplication.GRANT_PROFILE_PERMISSIONS_NOTIFICATION_ID);
//if (Build.VERSION.SDK_INT >= 23) {
StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
String tag = notification.getTag();
if ((tag != null) && tag.contains(PPApplication.GRANT_PROFILE_PERMISSIONS_NOTIFICATION_TAG+"_")) {
if (notification.getId() >= PPApplication.PROFILE_ID_NOTIFICATION_ID) {
notificationManager.cancel(notification.getTag(), notification.getId());
}
}
}
/*} else {
int notificationId = intent.getIntExtra("notificationId", 0);
manager.cancel(notificationId);
}*/
}
} catch (Exception e) {
PPApplication.recordException(e);
}
}
public static void cancelNotification(int notificationId) {
try {
final NotificationManager mNotifyMgr = (NotificationManager) xdrip.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(notificationId);
} catch (Exception e) {
//
}
}
private void stopSession() {
if (mp == null || mediaSession == null) {
return;
}
mp.stop();
mp.release();
mediaSession.release();
mediaSession.setActive(false);
mp = null;
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
}
private void notificationDismiss(Context ctx) {
final NotificationManager mNotifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
try {
mNotifyMgr.cancel(Notifications.exportAlertNotificationId);
} catch (NullPointerException e) {
UserError.Log.e(TAG,"Got null pointer in notificationDismiss !!");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big_text_main);
// Cancel Notification
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(StandaloneMainActivity.NOTIFICATION_ID);
// TODO: Handle and display reminder from your database
}
/**
* Callback from Form. No incoming messages can be processed through
* MessageReceived until the Form is initialized. Messages are cached
* until this method is called.
*/
@Override
public void onInitialize() {
Log.i(TAG, "onInitialize()");
isInitialized = true;
isRunning = true; // Added b/c REPL does not call onResume when starting Texting component
processCachedMessages();
NotificationManager nm = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(SmsBroadcastReceiver.NOTIFICATION_ID);
}
@Override
public void onResume() {
super.onResume();
// Check if we are currently connected, then open the player dialog
if ((mPdConnect == null || !mPdConnect.isShowing())
&& App.ClementineConnection != null
&& App.ClementineConnection.isConnected()) {
showPlayerDialog();
return;
}
// mDNS Discovery
mClementineMDns = new ClementineMDnsDiscovery(mHandler);
// Check if Autoconnect is enabled
if (mSharedPref.getBoolean(SharedPreferencesKeys.SP_KEY_AC, false) && doAutoConnect) {
// Post delayed, so the service has time to start
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
connect();
}
}, 250);
} else {
mClementineMDns.discoverServices();
}
doAutoConnect = true;
// Remove still active notifications
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(ClementineMediaSessionNotification.NOTIFIFCATION_ID);
mNotificationManager.cancel(DownloadManager.NOTIFICATION_ID_DOWNLOADS);
mNotificationManager.cancel(DownloadManager.NOTIFICATION_ID_DOWNLOADS_FINISHED);
}
private void updateMessageNotification() {
NotificationManager mNM = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
synchronized (pendingMessages) {
if (pendingMessages.size() == 0) {
mNM.cancel(MESSAGE_NOTIFICATION);
return;
}
String title = pendingMessages.get(0).getTitle();
// The PendingIntent to launch our activity if the user selects this notification
Intent i = new Intent(context, MessageActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);
String additional = pendingMessages.size() > 1 ? Localization.get("notifications.prompt.more", new String[]{String.valueOf(pendingMessages.size() - 1)}) : "";
Notification messageNotification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ERRORS_ID)
.setContentTitle(title)
.setContentText(Localization.get("notifications.prompt.details", new String[]{additional}))
.setSmallIcon(R.drawable.notification)
.setNumber(pendingMessages.size())
.setContentIntent(contentIntent)
.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(context, NotificationClearReceiver.class), 0))
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.build();
mNM.notify(MESSAGE_NOTIFICATION, messageNotification);
}
}
public static void deleteNotification(Context context, String pkgName) {
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.cancel(pkgName.hashCode());
deleteNotifying(context, pkgName);
}
}
@SuppressLint("NewApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
mDownloadUrl = intent.getStringExtra(EXTRA_DOWLOAD_URL);
if (!StringUtils.isEmpty(mDownloadUrl)){
if (FileUtils.isSDCardAvailable()){
destDir = new File(FileUtils.getSDCardPath());
if (destDir.exists()) {
File destFile = new File(destDir.getPath() + "/" + MD5Encryptor.GetMD5Code(mDownloadUrl));
if (destFile.exists()) {
destFile.delete();
}
}
}else {
Log.e(TAG, "AppUpgradeService sdcard not exist, can not download apk file");
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotification = new Notification();
mNotification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.app_upgrade_notification);
Intent completingIntent = new Intent();
completingIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
completingIntent.setClass(getApplication().getApplicationContext(), AppUpgradeService.class);
mPendingIntent = PendingIntent.getActivity(AppUpgradeService.this, R.string.app_name, completingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotification.icon = R.mipmap.ic_launcher;
mNotification.tickerText = getResources().getString(R.string.app_upgrade_download_begin);
mNotification.contentIntent = mPendingIntent;
mNotification.contentView.setProgressBar(R.id.app_upgrade_progressbar, 100, 0, true);
mNotification.contentView.setTextViewText(R.id.app_upgrade_progresstext, "0%");
mNotificationManager.cancel(mNotificationId);
mNotificationManager.notify(mNotificationId, mNotification);
new AppUpgradeThread().start();
}else{
Log.e(TAG, "AppUpgradeService download url is null");
stopSelf();
}
}else{
Log.e(TAG, "AppUpgradeService intent is null");
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
/**
* Use the update activity to update to a ccz with an invalid suite file.
* Assert that an error is shown and pinned notification is created w/ more details
*/
@Test
public void invalidUpdateTest() {
String invalidUpdateReference =
"jr://resource/commcare-apps/update_tests/invalid_suite_update/profile.ccpr";
// start the update activity
Intent updateActivityIntent =
new Intent(RuntimeEnvironment.application, UpdateActivity.class);
UpdateActivity updateActivity =
Robolectric.buildActivity(UpdateActivity.class, updateActivityIntent)
.setup().get();
// click the 'offline install' menu item
ShadowActivity shadowActivity = Shadows.shadowOf(updateActivity);
shadowActivity.clickMenuItem(UpdateActivity.MENU_UPDATE_FROM_CCZ);
// Make sure there are no pinned notifications before we start
NotificationManager notificationManager =
(NotificationManager)RuntimeEnvironment.application
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(CommCareNoficationManager.MESSAGE_NOTIFICATION);
Notification notification = Shadows.shadowOf(notificationManager)
.getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
assertNull(notification);
// mock receiving the offline app reference and start the update
Intent referenceIntent = new Intent();
referenceIntent.putExtra(InstallArchiveActivity.ARCHIVE_JR_REFERENCE, invalidUpdateReference);
shadowActivity.receiveResult(shadowActivity.getNextStartedActivity(), Activity.RESULT_OK,
referenceIntent);
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
// assert that we get the right error message
String errorMessage = (String)((TextView)updateActivity
.findViewById(R.id.update_progress_text)).getText();
assertEquals(Localization.get("updates.check.failed"), errorMessage);
// check that a pinned notification was created for invalid update
// NOTE: it is way more work to assert the notification body is correct, so skip over that
notification = Shadows.shadowOf(notificationManager)
.getNotification(CommCareNoficationManager.MESSAGE_NOTIFICATION);
assertNotNull(notification);
}
private void notificationDismiss(int notificationId) {
NotificationManager mNotifyMgr = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(notificationId);
}
@Override
public void hideAllNotifications() {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
}
public static void cancelNotification(Context context, int id) {
NotificationManager mNotifyMan = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMan.cancel(id);
}
private void Vibrate(Context ctx, AlertType alert, String bgValue, Boolean overrideSilent, int timeFromStartPlaying) {
Log.d(TAG, "Vibrate called timeFromStartPlaying = " + timeFromStartPlaying);
Log.d("ALARM", "setting vibrate alarm");
int profile = getAlertProfile(ctx);
// We use timeFromStartPlaying as a way to force vibrating/ non vibrating...
if (profile != ALERT_PROFILE_ASCENDING) {
// We start from the non ascending part...
timeFromStartPlaying = MAX_ASCENDING;
}
String title = bgValue + " " + alert.name;
String content = "BG LEVEL ALERT: " + bgValue;
Intent intent = new Intent(ctx, SnoozeActivity.class);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_action_communication_invert_colors_on)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(notificationIntent(ctx, intent))
.setDeleteIntent(snoozeIntent(ctx));
if (profile != ALERT_PROFILE_VIBRATE_ONLY && profile != ALERT_PROFILE_SILENT) {
if (timeFromStartPlaying >= MAX_VIBRATING) {
// Before this, we only vibrate...
float volumeFrac = (float)(timeFromStartPlaying - MAX_VIBRATING) / (MAX_ASCENDING - MAX_VIBRATING);
volumeFrac = Math.min(volumeFrac, 1);
if(profile == ALERT_PROFILE_MEDIUM) {
volumeFrac = (float)0.7;
}
Log.d(TAG, "Vibrate volumeFrac = " + volumeFrac);
boolean isRingTone = EditAlertActivity.isPathRingtone(ctx, alert.mp3_file);
if(isRingTone && !overrideSilent) {
builder.setSound(Uri.parse(alert.mp3_file));
} else {
if(overrideSilent || isLoudPhone(ctx)) {
PlayFile(ctx, alert.mp3_file, volumeFrac);
}
}
}
}
if (profile != ALERT_PROFILE_SILENT && alert.vibrate) {
builder.setVibrate(Notifications.vibratePattern);
}
NotificationManager mNotifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(Notifications.exportAlertNotificationId);
mNotifyMgr.notify(Notifications.exportAlertNotificationId, builder.build());
}
public void cancel(String tag, int identifier) {
NotificationManager notifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notifyManager.cancel(tag, identifier);
}