Android 通知栏发送消息Notification(可以使用自定义的布局)

来源:互联网 发布:淘宝详情页怎么消失啦 编辑:程序博客网 时间:2024/06/05 07:21

Android 通知栏
以下是图片:
这里写图片描述
以下是一个小 demo :
一个简单的应用场景:假如用户打开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"/>
2 0
原创粉丝点击