您的位置:首页 > 其它

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

2015-12-24 11:31 591 查看
Z2


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



一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。
package com.zzw.testnotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

private static final String TAG = "---->";

private final int NOTIFICATION_ID = 0xa01;
private final int REQUEST_CODE = 0xb01;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate");
}

@Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
}

@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
}

@Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
}

@Override
protected void onRestart() {
Log.d(TAG, "onRestart");
super.onRestart();
}

@Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
sendNotification(this, NOTIFICATION_ID, "这是标题", "这是内容");
}

//可当作发送通知栏消息模版使用
private void sendNotification(Context context, int notification_ID, String title, String content) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

//使用默认的通知栏布局
Builder builder = new Builder(context);
// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(title);
builder.setContentText(content);

Notification notification = builder.build();

/*    使用自定义的通知栏布局
*  当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
*/
// RemoteViews contentView = new RemoteViews(context.getPackageName(),
// R.layout.notification);
// contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher);
// contentView.setTextViewText(R.id.title, "土耳其和IS的秘密");
// contentView.setTextViewText(R.id.text, "土耳其拒绝向俄罗斯道歉,怀疑有IS撑腰");
// notification.contentView = contentView;

// 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
// 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
// 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;

// 点击notification自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL;

// 通知的时间
notification.when = System.currentTimeMillis();

// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(context, MainActivity.class);

// 当用户点击通知栏的Notification时候,切换回MainActivity。
PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.contentIntent = pi;

// 发送到手机的通知栏
notificationManager.notify(notification_ID, notification);
}

//可当作清除通知栏消息模版使用
private void deleteNotification(int id) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
}

需要注意的是,默认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文件源代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/text"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/imageView"
android:gravity="center_vertical"
android:singleLine="true"
android:text="TextView" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_alignLeft="@+id/title"
android:gravity="center_vertical"
android:singleLine="true"
android:text="TextView" />

</RelativeLayout>

notification.xml


由于sdk版本的不同,有的需要添加震动的权限:

<uses-permission android:name="android.permission.VIBRATE"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: