PendingIntent和Intent的区别
来源:互联网 发布:进入编程模式错误 编辑:程序博客网 时间:2024/06/16 16:35
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一个包装。
PendingIntent这个类封装了一个Intent(意图),它表示即将发送的动作的类型。 这个类的方法主要有两类,一类是getXXX,表示要激活的意图的类型,如activity,service或者是BroadcastReceiver等。另一类是send,表示立即发送这个意图。
下面是常用的API:1.
public
static
PendingIntent getActivity (Context context,
int
requestCode, Intent intent,
int
flags, Bundle options)
2.
public
static
PendingIntent getBroadcast (Context context,
int
requestCode, Intent intent,
int
flags)
3.
public
static
PendingIntent getService (Context context,
int
requestCode, Intent intent,
int
flags)
4.
public
void
send ()
示例: activity不贴了。 1.PendingIntent单独使用. 下面是activity中的一个button的点击事件所调用的方法.这个方法用于发送一个广播
01.
/**
02.
* 单独使用pendingIntent
03.
* 通过send方法发送intent
04.
*/
05.
public
void
pendingIntentFunc()
06.
{
07.
Intent intent =
new
Intent(
"com.xxx"
);
08.
intent.putExtra(
"info"
,
"我是info"
);
09.
//其实就跟调用sendBroadcast方法一样
10.
PendingIntent pi = PendingIntent.getBroadcast(
this
,
0
, intent, PendingIntent.FLAG_UPDATE_CURRENT);
11.
try
12.
{
13.
pi.send();
//执行PendingIntent中的意图
14.
}
catch
(CanceledException e)
15.
{
16.
e.printStackTrace();
17.
}
18.
}
01.
package
com.example.receiver;
02.
import
android.content.BroadcastReceiver;
03.
import
android.content.Context;
04.
import
android.content.Intent;
05.
import
android.util.Log;
06.
import
android.widget.Toast;
07.
public
class
MyReceiver
extends
BroadcastReceiver
08.
{
09.
private
static
final
String TAG =
"MyReceiver"
;
10.
@Override
11.
public
void
onReceive(Context context, Intent intent)
12.
{
13.
Log.i(TAG,
"成功收到广播..."
);
14.
Toast.makeText(context,
"info:"
+intent.getStringExtra(
"info"
),
0
).show();
15.
}
16.
}
清单文件中配置receiver:
1.
<receiver android:name=
"com.example.receiver.MyReceiver"
>
2.
<intent-filter >
3.
<action android:name=
"com.xxx"
/>
4.
</intent-filter>
5.
</receiver>
当点击按钮时,将会发出广播,而且还能接收到intent传来的信息。 logcat打印如下日志:
2.PendingIntent配合Notification使用 下面也是activity中的一个按钮的点击事件触发所执行的函数
01.
/**
02.
* 显示一个通知,点击通知将会激活一个服务
03.
*/
04.
public
void
sendNotification2()
05.
{
06.
NotificationManager manager = (NotificationManager)
this
.getSystemService(Context.NOTIFICATION_SERVICE);
07.
// notification的初始化
08.
Notification notification =
new
Notification();
09.
notification.icon = R.drawable.ic_launcher;
10.
notification.when = System.currentTimeMillis();
11.
notification.tickerText =
"又来一条新通知"
;
12.
notification.flags = notification.FLAG_AUTO_CANCEL;
13.
// 包装一个PendingIntent,当用户点击通知触发一个意图
14.
Intent intent =
new
Intent(
this
,MyService.
class
);
15.
// 点击通知将启动服务,相当于调用了startService方法
16.
PendingIntent contentIntent = PendingIntent.getService(
this
,
0
,intent, PendingIntent.FLAG_UPDATE_CURRENT);
17.
notification.setLatestEventInfo(
this
,
"标题"
,
"点我激活一个服务"
, contentIntent);
18.
// 激活通知
19.
manager.notify(
2
,notification);
//第一个参数代表的是通知的id
20.
}
定义一个服务:
01.
package
com.example.service;
02.
03.
import
android.app.Service;
04.
import
android.content.Intent;
05.
import
android.os.IBinder;
06.
import
android.util.Log;
07.
08.
public
class
MyService
extends
Service
09.
{
10.
private
static
final
String TAG =
"MyService"
;
11.
12.
@Override
13.
public
IBinder onBind(Intent intent)
14.
{
15.
return
null
;
16.
}
17.
18.
@Override
19.
public
void
onCreate()
20.
{
21.
Log.i(TAG,
"服务成功启动..."
);
22.
}
23.
}
清单文件中配置服务:
1.
<service android:name=
"com.example.service.MyService"
></service>
logcat成功打印日志,服务启动成功:
3.PendingIntent配合SmsManger使用 下面使用SmsManager发送一条短信。
1.
public
void
sendSMS()
2.
{
3.
SmsManager sm = SmsManager.getDefault();
4.
PendingIntent sentIntent = PendingIntent.getBroadcast(
this
,
0
,
new
Intent(
"com.xxx"
),PendingIntent.FLAG_UPDATE_CURRENT);
5.
sm.sendTextMessage(
"5556"
,
null
,
"hello world"
, sentIntent,
null
);
6.
}
另外5556也收到了短信:
上面基本把PendingIntent的使用场景都介绍了,api也使用了一些,其他api大家可以查文档。
- Intent和PendingIntent的区别
- PendingIntent和Intent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- PendingIntent和Intent的区别
- PendingIntent和Intent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- PendingIntent和Intent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- Intent和PendingIntent的区别
- 设计模式---单例模式(多线程下的单例模式)
- 自己实现字符串连接函数strcat
- Qt 生成一张图片
- 自己编写函数,实现库函数里的strcpy函数
- Java类集框架之LinkedList源码剖析
- PendingIntent和Intent的区别
- Android头像下拉缩放动效
- 黑马程序员——c语言基础:结构体(高级)
- jquery滚动公告demo
- 传输网络IP化
- 使用Hibernate需要导入的一些JAR包
- MVC上传文件
- Oracle 的 FORALL用法(批量增删改)
- 电源地线和大地的关系