2013-11-21 48 views
0

我目前正在開發其使用AlarmManager開始意圖展現給用戶每隔x分鐘一個應用程序。我還想在狀態欄中持續顯示直到下一個彈出窗口的時間。有沒有辦法顯示沒有後臺服務的持續通知。如果沒有,停止使用AlarmManager並僅使用服務每隔x分鐘就會彈出一次意圖是否合理?永久通知沒有服務

基本上我需要確保的意圖將在推出每x分鐘就算手機變低的MEM並且該通知保持在狀態欄中。我也希望能夠點擊通知來重置正在倒計時的計時器。達到這些目標的最佳方式是什麼?

感謝, 彌敦道

回答

1

要生成通知試試這個:

private void generateNotification(Context context, String message) { 

int icon = R.drawable.ic_launcher; 
long when = System.currentTimeMillis(); 
String appname = context.getResources().getString(R.string.app_name); 
NotificationManager notificationManager = (NotificationManager) context 
.getSystemService(Context.NOTIFICATION_SERVICE); 
int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
Notification notification; 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
new Intent(context, myactivity.class), 0); 

// To support 2.3 os, we use "Notification" class and 3.0+ os will use 
// "NotificationCompat.Builder" class. 
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { 
notification = new Notification(icon, message, 0); 
notification.setLatestEventInfo(context, appname, message, 
contentIntent); 
notification.flags = Notification.FLAG_AUTO_CANCEL; 
notificationManager.notify(0, notification); 

} else { 
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context); 
notification = builder.setContentIntent(contentIntent) 
.setSmallIcon(icon).setTicker(appname).setWhen(0) 
.setAutoCancel(true).setContentTitle(appname) 
.setContentText(message).build(); 

notificationManager.notify(0 , notification); 
} 
} 

希望這有助於。

+0

感謝您的幫助,我能當我的應用程序結束,雖然我猜通知將消失,從我的應用程序意圖的一個開始持續通知? – Nath5

+0

如果你點擊通知,將disappear.But如果有多個通知,如果你想手動刪除不是使用notificationmanager.cancelAll()方法的通知。 –