2010-10-19 20 views

回答

18

你應該能夠通知和NotificationManager做到這一點。然而,獲得一個有保證的方式來知道你的應用程序何時不運行是一個很難的部分。

你可以得到你是做一些像什麼希望的基本功能:

Notification notification = new Notification(R.drawable.your_app_icon, 
              R.string.name_of_your_app, 
              System.currentTimeMillis()); 
notification.flags |= Notification.FLAG_NO_CLEAR 
        | Notification.FLAG_ONGOING_EVENT; 
NotificationManager notifier = (NotificationManager) 
    context.getSystemService(Context.NOTIFICATION_SERVICE); 
notifier.notify(1, notification); 

此代碼必須在某個地方,你肯定會被解僱你的應用程序啓動時的位置。可能在應用程序的自定義應用程序對象的onCreate()方法中。

然而事情很棘手。殺死應用程序可能隨時發生。所以你可以嘗試在Application類的onTerminate()中放入一些東西,但不能保證被調用。

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1); 

將是什麼需要刪除圖標。

+5

'Notification'在API級別11中已棄用。請改用Notification.Builder。 – 2014-02-08 14:34:34

+0

如果您另外使用「服務」,則不必手動刪除通知。當服務停止時,通知將被自動刪除。 (「但是,如果您在前臺運行時停止服務,則通知也會被刪除。」https://developer.android.com/guide/components/services.html#Foreground) – 2016-06-23 11:46:38

6

看看開發指南「Creating Status Bar Notifications」。實現保持圖標出現在應用程序運行,只有當目標

一種方法是初始化通知中onCreate()並在onPause()方法只有isFinishing()返回true調用cancel(int)

一個例子:

private static final int NOTIFICATION_EX = 1; 
private NotificationManager notificationManager; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    notificationManager = (NotificationManager) 
     getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.notification_icon; 
    CharSequence tickerText = "Hello"; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, when); 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "My notification"; 
    CharSequence contentText = "Hello World!"; 
    Intent notificationIntent = new Intent(this, MyClass.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
     0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, 
     contentText, contentIntent); 

    notificationManager.notify(NOTIFICATION_EX, notification); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (isFinishing()) { 
     notificationManager.cancel(NOTIFICATION_EX); 
    } 
} 
+0

這隻會顯示給定的活動是否正在運行。如果應用程序仍在運行,則不會。 – 2010-10-20 03:37:46

+0

@Greg:對,這個示例代碼只能正確處理一個活動的應用程序。確定系統何時殺死應用程序要困難得多。正如你所提到的那樣,自定義的'Application'類可能是代碼的最佳位置,因爲它在整個應用程序的整個生命週期中一直存在。清除通知將很困難,因爲系統可能會在低內存條件下任意決定殺死應用程序的進程。您可以嘗試創建一個服務來監視狀態,因爲系統將在更多內存可用時嘗試重新啓動服務。 – Brian 2010-10-20 04:34:00

+0

如果從'onPause'中刪除'notificationManager.cancel(NOTIFICATION_EX)',則通知將保留在通知欄中,直到應用程序完全停止。 – 2015-04-13 08:50:26

8

對於新的API,你可以使用NotificationCompat.Builder -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.mipmap.ic_launcher) 
    .setContentTitle("Title"); 
Intent resultIntent = new Intent(this, MyActivity.class); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 
0, 
resultIntent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
Notification notification = mBuilder.build(); 
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotifyMgr.notify(NOTIFICATION_ID, notification); 

它會顯示,只要你的應用程序正在運行,並且有人手動關閉應用程序。你可以隨時取消你的通知 -

mNotifyMgr.cancel(NOTIFICATION_ID); 
+0

什麼將會是MyActivity.class的內容? @mjosh – 2017-08-14 18:47:20

+0

@Sarahcartenz任何你想要的真的 – mjosh 2017-08-14 21:44:53

+0

如果我不定義這樣一個類怎麼辦?或保持空。一旦通知被按下,我不想要一個動作。 – 2017-08-15 22:27:55

3

它確實有效。 我創建的方法上文所述的示例的:

private void applyStatusBar(String iconTitle, int notificationId) { 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
.setSmallIcon(R.mipmap.ic_launcher) 
.setContentTitle(iconTitle); 
Intent resultIntent = new Intent(this, ActMain.class); 
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
Notification notification = mBuilder.build(); 
notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT; 

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotifyMgr.notify(notificationId, notification);} 

應當稱爲像:applyStatusBar(「狀態欄測試」,10);

+0

如果我可能會問,ActMain.class的內容是什麼? – 2017-08-11 16:45:17

相關問題