![](/img/trans.png)
[英]Android Service start when push notification send and stop after service task complete
[英]Send notification on status bar when service is complete
我有一個正在使用服務下載圖像的應用程序,但是我希望在下載完成后通知用戶,即使用戶不在該應用程序之外(狀態欄中的通知),該應用程序也可以在minSdkVersion =“ 8”中使用,我知道對此有一些答案,但是我沒有得到通知的工作:這是我的一些代碼(僅通知有問題,它可以工作):
public class saveJsonContact1Service extends IntentService {
private static FileOutputStream fos;
private int result = Activity.RESULT_CANCELED;
public saveJsonContact1Service() {
super("saveJsonContact1Service");
}
@Override
protected void onHandleIntent(Intent intent) {
downloadImages();//To download images
sendNotification();//To send a notification after the donwloadImages() method has ended
}
private void sendNotification(){
CharSequence sns = "content";
CharSequence sns2 = "content";
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
notification.setLatestEventInfo(this, sns, sns2, pendingIntent);
notificationManager.notify(0, notification);
}
}
如何修改sendNotification()? 謝謝
嘗試這個:
private void sendNotification(){
CharSequence title = "The title";
CharSequence message = "My message";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(title);
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(0, mBuilder.build());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.