国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android notification 的總結(jié)分析

2020-02-21 17:24:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

不管是什么軟件,什么APP都需要不斷的進(jìn)行升級(jí),其實(shí)Android開發(fā)設(shè)計(jì)的時(shí)候也有很多個(gè)版本的,下面武林技術(shù)頻道小編帶大家愛一起了解android notification 的總結(jié)分析吧!

分類

?notification有以下幾種:

  1>普通notification

  

    1.內(nèi)容標(biāo)題

    2.大圖標(biāo)

    3.內(nèi)容

    4.內(nèi)容附加信息

    5.小圖標(biāo)

    6.時(shí)間

  2>大布局Notification

    圖1

  大布局notification是在android4.1以后才增加的,大布局notification與小布局notification只在‘7'部分有區(qū)別,其它部分都一致。大布局notification只有在所有notification的最上  面時(shí)才會(huì)顯示大布局,其它情況下顯示小布局。你也可以用手指將其擴(kuò)展為大布局(前提是它是大布局)。如下圖:

  圖2

    大布局notification有三種類型:如圖1為NotificationCompat.InboxStyle?類型。圖2左部為NotificationCompat.BigTextStyle。圖2右部 為:NotificationCompat.BigPictureStyle

?  3>自定義布局notification

?    除了系統(tǒng)提供的notification,我們也可以自定義notification。如下圖所示的一個(gè)音樂(lè)播放器控制notification:

    圖3

如何創(chuàng)建notification
?


?

    1>實(shí)例化一個(gè)NotificationCompat.Builder對(duì)象;如builder

     2>調(diào)用builder的相關(guān)方法對(duì)notification進(jìn)行上面提到的各種設(shè)置

    3>調(diào)用builder.build()方法此方法返回一個(gè)notification對(duì)象。

     4>實(shí)例化一個(gè)NotificationManager對(duì)象;如:manager

     5>調(diào)用manager的notify方法。

  注:

?  一個(gè)notification不必對(duì)上面所有的選項(xiàng)都進(jìn)行設(shè)置,但有3項(xiàng)是必須的:

   小圖標(biāo), set by?setSmallIcon()

  ? ?內(nèi)容標(biāo)題, set by?setContentTitle()

  ? ?內(nèi)容, set by?setContentText()

示例代碼
?



示例程序截圖:

?

?

0>初始化部分代碼

?

View Code
?public class MainActivity extends Activity implements OnClickListener {

???? private static final int NOTIFICATION_ID_1 = 0;
???? private static final int NOTIFICATION_ID_2 = 1;
???? private static final int NOTIFICATION_ID_3 = 2;
???? private static final int NOTIFICATION_ID_4 = 3;
???? private static final int NOTIFICATION_ID_5 = 4;
???? private static final int NOTIFICATION_ID_6 = 5;
???? private static final int NOTIFICATION_ID_7 = 6;
???? private static final int NOTIFICATION_ID_8 = 7;

???? private static int messageNum = 0;
???? private Context context = this;
???? private NotificationManager manager;
???? private Bitmap icon;
???? private static final int[] btns = new int[] { R.id.btn1, R.id.btn2,
???????????? R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8,
???????????? R.id.btn9 };

???? @Override
???? protected void onCreate(Bundle savedInstanceState) {
???????? super.onCreate(savedInstanceState);
???????? setContentView(R.layout.activity_main);
???????? init();
???? }

???? private void init() {
???????? // 獲取通知服務(wù)
???????? manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
???????? // 注冊(cè)監(jiān)聽器
???????? for (int btn : btns) {
???????????? findViewById(btn).setOnClickListener(this);
???????? }

???????? icon = BitmapFactory.decodeResource(getResources(),
???????????????? R.drawable.ic_launcher);
???? }

???? @Override
???? public void onClick(View v) {
???????? switch (v.getId()) {
???????? case R.id.btn1:
???????????? showNormal();
???????????? break;
???????? case R.id.btn2:
???????????? showBigView_Text();
???????????? break;
???????? case R.id.btn3:
???????????? showBigView_Pic();
???????????? break;
???????? case R.id.btn4:
???????????? showBigView_Inbox();
???????????? break;
???????? case R.id.btn5:
???????????? showCustomView();
???????????? break;
???????? case R.id.btn6:
???????????? backApp();
???????????? break;
???????? case R.id.btn7:
???????????? backScreen();
???????????? break;
???????? case R.id.btn8:
???????????? showProgressBar();
???????????? break;
???????? case R.id.btn9:
???????????? dismiss();
???????????? break;
???????? default:
???????????? Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
???????????? break;
???????? }
???? }

???? private void dismiss() {
???????? manager.cancelAll();
???? }


1>普通notification

?

?

?


View Code
???? private void showNormal() {

???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("showNormal").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setNumber(++messageNum)
???????????????? .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
???????????????? .build();
???????? manager.notify(NOTIFICATION_ID_1, notification);
???? }


2>大布局Text類型notification

?

?

?


View Code
?private void showBigView_Text() {
???????? NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
???????? textStyle
???????????????? .setBigContentTitle("BigContentTitle")
???????????????? .setSummaryText("SummaryText")
???????????????? .bigText(
???????????????????????? "I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("showBigView_Text").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setStyle(textStyle)
???????????????? .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
???????????????? .build();
???????? manager.notify(NOTIFICATION_ID_2, notification);
???? }


3> 大布局Picture類型notificatio

?

?

?


View Code
?private void showBigView_Pic() {
???????? NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
???????? pictureStyle.setBigContentTitle("BigContentTitle")
???????????????? .setSummaryText("SummaryText").bigPicture(icon);
???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("showBigView_Pic").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setStyle(pictureStyle)
???????????????? .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
???????????????? .build();
???????? manager.notify(NOTIFICATION_ID_3, notification);
???? }


4>大布局Inbox類型notification

?

?

?


View Code
?private void showBigView_Inbox() {
???????? NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
???????? inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
???????????????? "SummaryText");
???????? for (int i = 0; i < 5; i++)
???????????? inboxStyle.addLine("news:" + i);
???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("showBigView_Inbox").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setStyle(inboxStyle)
???????????????? .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
???????????????? .build();
???????? manager.notify(NOTIFICATION_ID_4, notification);
???? }


5>自定義notification
效果圖:

?



并對(duì)中間的播放按鈕做了一個(gè)簡(jiǎn)單的點(diǎn)擊處理事件(點(diǎn)擊播放后,請(qǐng)關(guān)閉幕簾否則可能會(huì)看不到toast提示)

?

View Code
?private void showCustomView() {
???????? RemoteViews remoteViews = new RemoteViews(getPackageName(),
???????????????? R.layout.custom_notification);
???????? Intent intent = new Intent(this, TestMusicControl.class);
???????? PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
???????????????? intent, 0);
???????? remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,
???????????????? pendingIntent);
???????? NotificationCompat.Builder builder = new Builder(context);
???????? builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon)
???????????????? .setLargeIcon(icon).setOngoing(true)
???????????????? .setTicker("music is playing");
???????? manager.notify(NOTIFICATION_ID_8, builder.build());
???? }


布局文件:

?

?

?


View Code
?<?xml version="1.0" encoding="utf-8"?>
?<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
???? android:layout_width="match_parent"
???? android:layout_height="match_parent"
???? android:gravity="center_vertical"
???? android:orientation="horizontal" >

???? <ImageView
???????? android:id="@+id/songer_pic"
???????? android:layout_width="64dp"
???????? android:layout_height="64dp"
???????? android:src="@drawable/yan" />

???? <LinearLayout
???????? android:layout_width="fill_parent"
???????? android:layout_height="fill_parent"
???????? android:gravity="center_vertical"
???????? android:orientation="horizontal" >

???????? <ImageView
???????????? android:id="@+id/last_music"
???????????? android:layout_width="0dp"
???????????? android:layout_height="48dp"
???????????? android:layout_weight="1"
???????????? android:src="@drawable/music_previous" />

???????? <ImageView
???????????? android:id="@+id/paly_pause_music"
???????????? android:layout_width="0dp"
???????????? android:layout_height="48dp"
???????????? android:layout_weight="1"
???????????? android:src="@drawable/music_play" />

???????? <ImageView
???????????? android:id="@+id/next_music"
???????????? android:layout_width="0dp"
???????????? android:layout_height="48dp"
???????????? android:layout_weight="1"
???????????? android:src="@drawable/music_next" />
???? </LinearLayout>

?</LinearLayout>


帶進(jìn)度條的notification

?

?

?


View Code
???? private void showProgressBar() {

???????? final NotificationCompat.Builder builder = new NotificationCompat.Builder(
???????????????? context);
???????? builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("showProgressBar").setContentInfo("contentInfo")
???????????????? .setOngoing(true).setContentTitle("ContentTitle")
???????????????? .setContentText("ContentText");
???????? new Thread(new Runnable() {

???????????? @Override
???????????? public void run() {

???????????????? int progress = 0;

???????????????? for (progress = 0; progress < 100; progress += 5) {
???????????????????? //將setProgress的第三個(gè)參數(shù)設(shè)為true即可顯示為無(wú)明確進(jìn)度的進(jìn)度條樣式
???????????????????? builder.setProgress(100, progress, false);
???????????????????? manager.notify(NOTIFICATION_ID_7, builder.build());
???????????????????? try {
???????????????????????? // Sleep for 5 seconds
???????????????????????? Thread.sleep(2 * 1000);
???????????????????? } catch (InterruptedException e) {
???????????????????????? System.out.println("sleep failure");
???????????????????? }
???????????????? }
???????????????? builder.setContentTitle("Download complete")
???????????????????????? .setProgress(0, 0, false).setOngoing(false);
???????????????? manager.notify(NOTIFICATION_ID_7, builder.build());

???????????? }
???????? }).start();
???? }


點(diǎn)擊事件處理

--------------------------------------------------------------------------------
  有時(shí)候我們可能需要實(shí)現(xiàn)這樣的功能:當(dāng)新notification出現(xiàn)時(shí),我們希望點(diǎn)擊它后可直接進(jìn)入應(yīng)用相應(yīng)的界面中去完整查看或處理此消息的功能。然后,當(dāng)我們點(diǎn)擊back按鈕時(shí)返回到應(yīng)用主界面而不是桌面。比如:當(dāng)我們有新的短信來(lái)時(shí),我們?cè)谌蝿?wù)欄中點(diǎn)擊它后進(jìn)入讀信息頁(yè)面,當(dāng)我們讀完短信后,按“返回”鍵回到短信的主界面,而不是手機(jī)桌面。要實(shí)現(xiàn)這樣的功能要我們做相應(yīng)的處理:

?

1>返回應(yīng)用主界面

?

View Code
???? private void backApp() {

???????? TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
???????? // Adds the back stack
???????? stackBuilder.addParentStack(OtherActivity.class);
???????? // Adds the Intent to the top of the stack
???????? Intent resultIntent = new Intent(this, OtherActivity.class);
???????? stackBuilder.addNextIntent(resultIntent);
???????? // Gets a PendingIntent containing the entire back stack
???????? PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
???????????????? PendingIntent.FLAG_UPDATE_CURRENT);

???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("backApp").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setContentIntent(resultPendingIntent).setAutoCancel(true)
???????????????? .setDefaults(Notification.DEFAULT_ALL).build();
???????? manager.notify(NOTIFICATION_ID_5, notification);
???????? this.finish();
???? }


并需要我們?cè)谂渲梦募袑?duì)我們用來(lái)顯示詳細(xì)信息的OtherActivity進(jìn)行相應(yīng)的配置如下:

?

?

?


<activity
???????????? android:name="com.example.notification.OtherActivity"
???????????? android:label="@string/title_activity_other"
???????????? android:parentActivityName=".MainActivity" >
???????????? <meta-data
???????????????? android:name="android.support.PARENT_ACTIVITY"
???????????????? android:value=".MainActivity" />
???????? </activity>


2>直接返回桌面

?

?  有些時(shí)候我們可能需要實(shí)現(xiàn)這樣的功能:當(dāng)我們點(diǎn)擊notification時(shí)彈出一個(gè)稍大點(diǎn)的窗口來(lái)顯示整個(gè)消息,這窗口的作用就是用來(lái)顯示整個(gè)消息內(nèi)容的,和此應(yīng)用內(nèi)的其它Activity都沒(méi)有關(guān)系,然后當(dāng)我們點(diǎn)擊"back"后直接返回到手機(jī)桌面。要實(shí)現(xiàn)這樣的功能我們只需要調(diào)用builder的.setContentIntent方法,然后對(duì)所要跳轉(zhuǎn)到的activity在配置文件中進(jìn)行一些配置:

?

View Code
?private void backScreen() {
???????? Intent notifyIntent = new Intent(this, SpecialActivity.class);
???????? // Sets the Activity to start in a new, empty task
???????? notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
???????????????? | Intent.FLAG_ACTIVITY_CLEAR_TASK);
???????? // Creates the PendingIntent
???????? PendingIntent notify_Intent = PendingIntent.getActivity(this, 0,
???????????????? notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

???????? Notification notification = new NotificationCompat.Builder(context)
???????????????? .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
???????????????? .setTicker("backScreen").setContentInfo("contentInfo")
???????????????? .setContentTitle("ContentTitle").setContentText("ContentText")
???????????????? .setContentIntent(notify_Intent).setAutoCancel(true)
???????????????? .setDefaults(Notification.DEFAULT_ALL).build();
???????? manager.notify(NOTIFICATION_ID_6, notification);
???????? this.finish();
???? }


配置文件:

?

?

?


?<activity
???????????? android:name="com.example.notification.SpecialActivity"
???????????? android:excludeFromRecents="true"
???????????? android:label="@string/title_activity_special"
???????????? android:launchMode="singleTask"
???????????? android:taskAffinity="" >

以上的介紹就是android notification 的總結(jié)分析,現(xiàn)在市場(chǎng)上的知識(shí)多種多樣,我們?cè)谶x擇的時(shí)候要了解清楚,希望武林技術(shù)頻道小編能幫助到您。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丰顺县| 会昌县| 边坝县| 延津县| 灵武市| 海阳市| 新蔡县| 西峡县| 泰安市| 浙江省| 海宁市| 日土县| 藁城市| 营口市| 偏关县| 桐城市| 永仁县| 苗栗市| 和静县| 阳春市| 淮南市| 固安县| 红河县| 乡城县| 启东市| 江北区| 交口县| 五华县| 米易县| 米脂县| 大同县| 兴城市| 嵊泗县| 淮北市| 广西| 简阳市| 江山市| 康定县| 舟曲县| 锦州市| 遂川县|