通知欄發送訊息Notification(可以使用自訂的布局),自訂notification

來源:互聯網
上載者:User

通知欄發送訊息Notification(可以使用自訂的布局),自訂notification

一個簡單的應用情境:假如使用者開啟Activity以後,按Home鍵,此時Activity 進入-> onPause() -> onStop() 不可見。代碼在此時機發送一個Notification到通知欄。當使用者點擊通知欄的Notification後,又重新onRestart() -> onStart() -> onResume() 切換回原Activity。

  1 package com.zzw.testnotification;  2   3 import android.app.Activity;  4 import android.app.Notification;  5 import android.app.NotificationManager;  6 import android.app.PendingIntent;  7 import android.content.Context;  8 import android.content.Intent;  9 import android.os.Bundle; 10 import android.support.v4.app.NotificationCompat.Builder; 11 import android.util.Log; 12 import android.widget.RemoteViews; 13  14 public class MainActivity extends Activity { 15  16     private static final String TAG = "---->"; 17  18     private final int NOTIFICATION_ID = 0xa01; 19     private final int REQUEST_CODE = 0xb01; 20  21     @Override 22     protected void onCreate(Bundle savedInstanceState) { 23         super.onCreate(savedInstanceState); 24         setContentView(R.layout.activity_main); 25         Log.d(TAG, "onCreate"); 26     } 27  28     @Override 29     protected void onResume() { 30         Log.d(TAG, "onResume"); 31         super.onResume(); 32     } 33  34     @Override 35     protected void onDestroy() { 36         Log.d(TAG, "onDestroy"); 37         super.onDestroy(); 38     } 39  40     @Override 41     protected void onPause() { 42         Log.d(TAG, "onPause"); 43         super.onPause(); 44     } 45  46     @Override 47     protected void onRestart() { 48         Log.d(TAG, "onRestart"); 49         super.onRestart(); 50     } 51  52     @Override 53     protected void onStart() { 54         Log.d(TAG, "onStart"); 55         super.onStart(); 56     } 57  58     @Override 59     protected void onStop() { 60         super.onStop(); 61         Log.d(TAG, "onStop"); 62         sendNotification(this, NOTIFICATION_ID, "這是標題", "這是內容"); 63     } 64  65      66     //可當作發送通知欄訊息模版使用 67     private void sendNotification(Context context, int notification_ID, String title, String content) { 68         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 69  70         //使用預設的通知欄布局 71         Builder builder = new Builder(context); 72         // 此處設定的表徵圖僅用於顯示新提醒時候出現在裝置的通知欄 73         builder.setSmallIcon(R.drawable.ic_launcher); 74         builder.setContentTitle(title); 75         builder.setContentText(content); 76  77         Notification notification = builder.build(); 78  79         /*    使用自訂的通知欄布局 80          *  當使用者下來通知欄時候看到的就是RemoteViews中自訂的Notification布局 81          */ 82         // RemoteViews contentView = new RemoteViews(context.getPackageName(), 83         // R.layout.notification); 84         // contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher); 85         // contentView.setTextViewText(R.id.title, "土耳其和IS的秘密"); 86         // contentView.setTextViewText(R.id.text, "土耳其拒絕向俄羅斯道歉,懷疑有IS撐腰"); 87         // notification.contentView = contentView; 88  89         // 發送通知到通知欄時:提示聲音 + 手機震動 + 點亮Android手機呼吸燈。 90         // 注意!!(提示聲音 + 手機震動)這兩項基本上Android手機均支援。 91         // 但Android呼吸燈能否點亮則取決於各個手機硬體製造商自家的設定。 92         notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS; 93  94         // 點擊notification自動消失 95         notification.flags = Notification.FLAG_AUTO_CANCEL; 96  97         // 通知的時間 98         notification.when = System.currentTimeMillis(); 99 100         // 需要注意的是,作為選項,此處可以設定MainActivity的啟動模式為singleTop,避免重複建立onCreate()。101         Intent intent = new Intent(context, MainActivity.class);102 103         // 當使用者點擊通知欄的Notification時候,切換回MainActivity。104         PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);105         notification.contentIntent = pi;106 107         // 發送到手機的通知欄108         notificationManager.notify(notification_ID, notification);109     }110 111     //可當作清除通知欄訊息模版使用112     private void deleteNotification(int id) {113         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);114         notificationManager.cancel(id);115     }116 }

需要注意的是,預設Android的Activity為標準模式,即每次都new一個新的Activity出來,不是原先的Activity,在本例中,可以觀察到MainActivity中的onCreate()如果不修改啟動模式,則每次本調用每次TextView顯示的時間不同(遞增),所有為了使用原來的Activity、避免重複new一個新的出來,需要:

在AndroidManifest.xml中修改MainActivity啟動模式為:singleTop

<activity            android:name=".MainActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

notification.xml檔案原始碼:

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/imageView" 8 android:layout_width="50dp" 9 android:layout_height="50dp"10 android:layout_alignParentLeft="true"11 android:layout_centerVertical="true"12 android:src="@drawable/ic_launcher" />13 14 <TextView15 android:id="@+id/title"16 android:layout_width="wrap_content"17 android:layout_height="wrap_content"18 android:layout_above="@+id/text"19 android:layout_alignParentRight="true"20 android:layout_alignTop="@+id/imageView"21 android:layout_marginLeft="18dp"22 android:layout_toRightOf="@+id/imageView"23 android:gravity="center_vertical"24 android:singleLine="true"25 android:text="TextView" />26 27 <TextView28 android:id="@+id/text"29 android:layout_width="wrap_content"30 android:layout_height="wrap_content"31 android:layout_alignBottom="@+id/imageView"32 android:layout_alignLeft="@+id/title"33 android:gravity="center_vertical"34 android:singleLine="true"35 android:text="TextView" />36 37 38 </RelativeLayout>notification.xml

由於sdk版本的不同,有的需要添加震動的許可權:

<uses-permission android:name="android.permission.VIBRATE"/>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.