Intent和PendingIntent的区别

从网络上总结Intent和PendingInent的区别:

intent英文意思是意图pending表示即将发生或来临的事情
PendingIntent这个类用于处理即将发生的事情。比如在通知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一个包装。

Android中的消息通知Notification和NotificationManager

Notification,即通知,一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。

已添加的Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。

先来区分以下状态栏和状态条的区别:

1、状态条就是手机屏幕最上方的一个条形状的区域;

在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

2、状态栏就是手从状态条滑下来的可以伸缩的view;

在状态栏中一般有两类(使用FLAG_标记):

(1)正在进行的程序;

(2)是通知事件;

通常情况下发布一条Notification分4个步骤:

1、通过getSystemService()方法得到NotificationManager对象;

nManager = (NotificationManager) this.getSystemService(service);

2、对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;

notification.icon = R.drawable.ic_launcher;// 设置通知的图标
notification.tickerText = tickerText; // 显示在状态栏中的文字
notification.when = when; // 设置来通知时的时间

3、通过NotificationManager对象的notify()方法来执行一个notification的消息;

// 发出通知
nManager.notify(ID, notification);

4、取消一个Notification

nManager.cancel(ID);

Notification的一些其他特性:

/*

* 添加声音

* notification.defaults |=Notification.DEFAULT_SOUND;

* 或者使用以下几种方式

* notification.sound = Uri.parse(“file:///sdcard/notification/ringer.mp3”);

* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, “6”);

* 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加”FLAG_INSISTENT”

* 如果notification的defaults字段包括了”DEFAULT_SOUND”属性,则这个属性将覆盖sound字段中定义的声音

*/

/*

* 添加振动

* notification.defaults |= Notification.DEFAULT_VIBRATE;

* 或者可以定义自己的振动模式:

* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒

* notification.vibrate = vibrate;

* long数组可以定义成想要的任何长度

* 如果notification的defaults字段包括了”DEFAULT_VIBRATE”,则这个属性将覆盖vibrate字段中定义的振动

*/

/*

* 添加LED灯提醒

* notification.defaults |= Notification.DEFAULT_LIGHTS;

* 或者可以自己的LED提醒模式:

* notification.ledARGB = 0xff00ff00;

* notification.ledOnMS = 300; //亮的时间

* notification.ledOffMS = 1000; //灭的时间

* notification.flags |= Notification.FLAG_SHOW_LIGHTS;

*/

/*

* 更多的特征属性

* notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知

* notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知

* notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的”Ongoing”即”正在运行”组中

* notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的”清除通知”后,此通知不清除,

* //经常与FLAG_ONGOING_EVENT一起使用

* notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部

* //如果要使用此字段,必须从1开始

* notification.iconLevel = ; //

*/

 

示例代码如下:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

private Button sendBtn, cancelBtn;
private Notification notification;
private NotificationManager nManager;
private Intent intent;
private PendingIntent pIntent;
// Notification的标示ID
private static final int ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//测试NotificationManager
sendBtn = (Button) this.findViewById(R.id.send);
cancelBtn = (Button) this.findViewById(R.id.cancel);

String service = NOTIFICATION_SERVICE;
nManager = (NotificationManager) this.getSystemService(service);

notification = new Notification();
String tickerText = “aibb测试Notifaction”; // 通知提示
// 显示时间
long when = System.currentTimeMillis();

notification.icon = R.drawable.ic_launcher;// 设置通知的图标
notification.tickerText = tickerText; // 显示在状态栏中的文字
notification.when = when; // 设置来通知时的时间
notification.sound = Uri.parse(“android.resource://com.sun.alex/raw/startsuccess”); // 自定义声音
notification.flags = Notification.FLAG_NO_CLEAR; // 点击清除按钮时就会清除消息通知,但是点击通知栏的通知时不会消失
notification.flags = Notification.FLAG_ONGOING_EVENT; // 点击清除按钮不会清除消息通知,可以用来表示在正在运行
notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失
// notification.flags |= Notification.FLAG_INSISTENT; // 一直进行,比如音乐一直播放,知道用户响应
notification.defaults = Notification.DEFAULT_SOUND; // 调用系统自带声音
notification.defaults = Notification.DEFAULT_SOUND;// 设置默认铃声
notification.defaults = Notification.DEFAULT_VIBRATE;// 设置默认震动
notification.defaults = Notification.DEFAULT_ALL; // 设置铃声震动
notification.defaults = Notification.DEFAULT_ALL; // 把所有的属性设置成默认
notification.ledOnMS = 300; //LED亮的时间

sendBtn.setOnClickListener(sendClickListener);
cancelBtn.setOnClickListener(cancelClickListener);
}

private OnClickListener sendClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 单击通知后会跳转到NotificationResult类
intent = new Intent(MainActivity.this,
NotificationResultActivity.class);
// 获取PendingIntent,点击时发送该Intent
pIntent = PendingIntent.getActivity(MainActivity.this, 0,
intent, 0);
// 设置通知的标题和内容
notification.setLatestEventInfo(MainActivity.this, “标题-测试”,
“内容-测试”, pIntent);
// 发出通知
nManager.notify(ID, notification);
}
};

private OnClickListener cancelClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// 取消通知
nManager.cancel(ID);
}
};
}