Android 原生通知Notification 写法
Notification
是個進程間的通訊
手機狀態欄的提示.出現在桌面通知欄里,他不在我們的App里,而是由SystemUI進程顯示的提示.所以讓另外一個系統的進程SystemUI幫我們顯示一個通知欄提醒.
不多說上代碼
屬性配置
?<uses-permission android:name="android.permission.VIBRATE" />
主要代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? ? private Button button;
? ? private Button button2;
? ? private NotificationManager notificationManager;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? initView();
? ? }
? ? private void initView() {
? ? ? ? button = (Button) findViewById(R.id.button);
? ? ? ? button.setOnClickListener(this);
? ? ? ? button2 = (Button) findViewById(R.id.button2);
? ? ? ? button2.setOnClickListener(this);
? ? }
? ? /**
? ? ?* 讓另外一個系統的進程SystemUI幫我們顯示一個通知欄提醒
? ? ?* 進程間的通訊
? ? ?*
? ? ?* @param v
? ? ?*/
? ? @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.button:
? ? ? ? ? ? ? ? //進程間通信.主要是把一個動作交給另外一個應用程序來做的意圖,就用PendingIntent包裹一下.
? ? ? ? ? ? ? ? //pendingIntent,可以打開一個四大組件,用其靜態方法,get....
? ? ? ? ? ? ? ? //getActivity,1 上下文,2 請求碼用不到就為0,3 意圖對象,4 指定其點擊后的狀態標識
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? intent.setAction(Intent.ACTION_DIAL);
? ? ? ? ? ? ? ? intent.setData(Uri.parse("tel://110"));
// ? ? ? ? ? ? ? ?Intent intent = new Intent(this,JumpActivity.class);//跳轉到指定的Activity,不用StartActivity.
? ? ? ? ? ? ? ? PendingIntent pendingIntent= ?PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
? ? ? ? ? ? ? ? //獲取系統的通知服務,上面的Intent操作就是用戶點擊通知欄時,自動進入撥號界面,
? ? ? ? ? ? ? ? notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
? ? ? ? ? ? ? ? //直接new出來,該方式在安卓3.0以后已經過時了
// ? ? ? ? ? ? ? ?Notification notification=new Notification();
? ? //使用鏈式調用的方法,創建Notification對象的同時往里面進行設置,是主流的創建方式,Builder的參數是上下文.
? ? ? ? ? ? ? ? Notification notification = new Notification.Builder(this)
? ? ? ? ? ? ? ? ? ? ? ? //設置通知欄的標題
? ? ? ? ? ? ? ? ? ? ? ? .setContentTitle("你中獎啦")
? ? ? ? ? ? ? ? ? ? ? ? //設置通知欄的通知內容
? ? ? ? ? ? ? ? ? ? ? ? .setContentText("快來110 去領取!!!")
? ? ? ? ? ? ? ? ? ? ? ? //設置通知欄的圖片(就是通知一來,在顯示電量那一行出現的小圖標)
? ? ? ? ? ? ? ? ? ? ? ? .setSmallIcon(R.mipmap.ic_launcher)
? ? ? ? ? ? ? ? ? ? ? ? //設置通知欄的大圖片(就是通知欄拉下來顯示的圖標)
? ? ? ? ? ? ? ? ? ? ? ? .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
? ? ? ? ? ? ? ? ? ? ? ? //設置通知欄被點擊后會執行的意圖(比如跳轉到指定的activity,比如打電話等等)
? ? ? ? ? ? ? ? ? ? ? ? .setContentIntent(pendingIntent)
? ? ? ? ? ? ? ? ? ? ? ? //當通知欄提示被點擊執行時,通知欄會消失在桌面,不設置此方法,默認通知欄提示被點擊后依然存在.所以必用此方法.
? ? ? ? ? ? ? ? ? ? ? ? .setAutoCancel(true)
// ? ? ? ? ? ? ? ? ? ? ? ?.setSound(Uri.parse("")) //設置通知欄提示到來時的聲音是什么.
? ? ? ? ? ? ? ? ? ? ? ? //設置當通知欄提示到來時,手機每一次震動的時長.使用此功能記著加權限.<uses-permission android:name="android.permission.VIBRATE"/>
// ? ? ? ? ? ? ? ? ? ? ? ?.setVibrate(new long[]{100,200,300}) //不設置沒有震動
? ? ? ? ? ? ? ? ? ? ? ? // 使用系統默認聲音,震動,led燈等設置
// ? ? ? ? ? ? ? ? ? ? ? ?.setDefaults(Notification.DEFAULT_ALL)
? ? ? ? ? ? ? ? ? ? ? ? .build();
? ? ? ? ? ? ? ? //FLAG_NO_CLEAR 使通知欄提示取消不掉,一直存在. ? FLAG_Auto_CLEAR 通知欄提示只要被點擊一次就不會存在.
? ? ? ? ? ? ? ? notification.flags=Notification.FLAG_NO_CLEAR;
? ? ? ? ? ? ? ? //讓通知顯示在狀態欄里.參數1,給通知起的ID,方便對其單獨的操作(比如用cancel把指定通知取消掉) 參數2,就是Notification對象.
? ? ? ? ? ? ? ? notificationManager.notify(1, notification);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button2:
? ? ? ? ? ? ? ? //使用NotificationManager對象,取消掉指定int標識的通知提示.
? ? ? ? ? ? ? ? notificationManager.cancel(1);
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
總結
以上是生活随笔為你收集整理的Android 原生通知Notification 写法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RecyclerView的版本要和app
- 下一篇: Android百度地图显示空白(只有格子