[安卓開發基礎] Notification 顯示通知
實現步驟:
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Intent 是及時啟動,intent 隨所在的activity 消失而消失。
PendingIntent 可以看作是對intent的包裝,通常通過getActivity,getBroadcast ,getService來得到pendingintent的例項,當前activity並不能馬上啟動它所包含的intent,而是在外部執行 pendingintent時,呼叫intent的。正由於pendingintent中 儲存有當前App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執行pendingintent裡的 Intent, 就算在執行時當前App已經不存在了,也能通過存在pendingintent裡的Context照樣執行Intent。另外還可以處理intent執行後的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞資料,而Pendingintent,一般用在 Notification上,可以理解為延遲執行的intent,PendingIntent是對Intent一個包裝。
---------------------
作者:笑對生活_展望未來
來源:CSDN
原文:https://blog.csdn.net/zeng622peng/article/details/6180190
版權宣告:本文為博主原創文章,轉載請附上博文連結!
-----------------------------------------------------------------------------------------------------------------------------
xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/notifiy_background" tools:context="com.zengjx.androidbaseproject.NotificationActivity"> </android.support.constraint.ConstraintLayout>
package com.zengjx.androidbaseproject; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class NotificationActivity extends AppCompatActivity { final int NOTIFIDED=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); //建立 並且通知 NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE); //建立一個通知物件 // Notification.Builder notification =new Notification.Builder(NotificationActivity.this); Notification.Builder notification = new Notification.Builder(this); // 建立一個Notification物件 //設定屬性 // 設定開啟該通知,該通知自動消失 notification.setAutoCancel(true);//點選後 會自動消失。 notification.setSmallIcon(R.drawable.packet);//訊息標誌圖片 notification.setContentText("點選檢視詳情");//設定內容 notification.setContentTitle("獎勵百萬紅包");//設定標題 notification.setWhen(System.currentTimeMillis());//設定傳送時間,通知的時間,一般為系統時間,也可以使用setWhen()設定 notification.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);//預設聲音 //intent英文意思是意圖,pending表示即將發生或來臨的事情。 //PendingIntent這個類用於處理即將發生的事情。比如在通知Notification中用於跳轉頁面,但不是馬上跳轉。 Intent intent =new Intent(NotificationActivity.this,NotificationDetailActivity.class); PendingIntent pendingIntent =PendingIntent.getActivity(NotificationActivity.this,0,intent,0); notification.setContentIntent(pendingIntent);//設定通知欄跳轉 notificationManager.notify(NOTIFIDED,notification.build()); } }
詳細頁面:
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img"
tools:context="com.zengjx.androidbaseproject.NotificationDetailActivity">
</android.support.constraint.ConstraintLayout>
package com.zengjx.andro:idbaseproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NotificationDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notificationdetail);
}
}
效果: