今天使用4.0.3使用
Notification notification2 = new Notification(R.drawable.advise2, "通知測試", System.currentTimeMillis()); notification2.setLatestEventInfo(getActivity(), "testTitle", "testContent", null);
結果androidstudio報錯,setLatestEventInfo該方法找不到,經過查證官方在API Level 11中,該函數已經被替代,不推薦使用了。古在4.0.3平台也就是API Level 15中,使用Notification的setLatestEventInfo()函數時,顯示setLatestEventInfo()效果。建議使用Notification.Builder來創建 notification 實例
Notification.Builder builder1 = new Notification.Builder(MainActivity.this); builder1.setSmallIcon(R.drawable.advise2); //設置圖標 builder1.setTicker("顯示第二個通知"); builder1.setContentTitle("通知"); //設置標題 builder1.setContentText("點擊查看詳細內容"); //消息內容 builder1.setWhen(System.currentTimeMillis()); //發送時間 builder1.setDefaults(Notification.DEFAULT_ALL); //設置默認的提示音,振動方式,燈光 builder1.setAutoCancel(true);//打開程序后圖標消失 Intent intent =new Intent (MainActivity.this,Center.class); PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0); builder1.setContentIntent(pendingIntent); Notification notification1 = builder1.build(); notificationManager.notify(124, notification1); // 通過通知管理器發送通知如果該通知只是起到 “通知”的作用,不希望用戶點擊后有相應的跳轉,那么,intent,pendingIntent這幾行代碼可以不寫
Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.advise); builder.setTicker("顯示第一個通知"); builder.setContentTitle("第一個通知"); builder.setContentText("每天進步一點點"); builder.setWhen(System.currentTimeMillis()); //發送時間 builder.setDefaults(Notification.DEFAULT_ALL); Notification notification = builder.build(); notificationManager.notify(123, notification);
第一個具有點擊提示有跳轉功能,后面一個沒有跳轉功能,只是提示作用
以下借鑒其他博主的總結:
在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,現在網上大多數資料還是API Level 11版本前的用法介紹,如果不熟悉的話,會繞一些彎路。
現在總結如下,希望對以后使用的程序員有所幫助。
低於API Level 11版本,也就是Android 2.3.3以下的系統中,setLatestEventInfo()函數是唯一的實現方法。前面的有關屬性設置這里就不再提了,網上資料很多。
Intent intent = new Intent(this,MainActivity);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
manager.notify(id, notification);
高於API Level 11,低於API Level 16 (Android 4.1.2)版本的系統中,可使用Notification.Builder來構造函數。但要使用getNotification()來使notification實現。此時,前面版本在notification中設置的Flags,icon等屬性都已經無效,要在builder里面設置。PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
manager.notify(id, notification);
Notification.Builder builder = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
高於API Level 16的版本,就可以用Builder和build()函數來配套的方便使用notification了。.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
【
注意點
】:
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
在構造notification的時候有很多種寫法,但是要注意,用
Notification notification = new Notification();
這種構建方法的時候,一定要加上notification.icon這個設置,不然,程序雖然不會報錯,但是會沒有效果。
======================================================================
實際代碼如下:
服務代碼
TimerService.class
package com.adisoon.appservicedemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
/**
* 延遲1秒在標題欄顯示通知
* Created by nimorl on 2017-10-19.
*/
public class TimerService extends Service {
private Timer timer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
timer = new Timer(true);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
timer.schedule(new TimerTask() {
@Override
public void run() {
String ns = Context.NOTIFICATION_SERVICE;
//獲得通知管理器
NotificationManager manager = (NotificationManager) getSystemService(ns);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.warning);
builder.setTicker("顯示第一個通知");
builder.setContentTitle("第一個通知");
builder.setContentText("有病毒正在刪除你的文件");
builder.setWhen(System.currentTimeMillis()); //設置時間
Notification notification = builder.build();
//定義通知行為
manager.notify(0,notification);
TimerService.this.stopSelf();
}
},6000);
}
}
主界面調用
startService(new Intent(this,TimerService.class));