PendingIntent这个类封装了一个Intent(意图),它表示即将发送的动作的类型。
定义一个BroadcastReceiver用于接收广播
:
清单文件中配置receiver:
定义一个服务:
清单文件中配置服务:
当点击任务栏通知即开启服务:
PendingIntent作为senTextMessage的参数被传递进来,作为短信发送成功的回执,此时PendingIntent会发送一个广播。还是以上面例子的广播接收者接收广播,可以看到logcat打印了日志:
上面基本把PendingIntent的使用场景都介绍了,api也使用了一些,其他api大家可以查文档。
这个类的方法主要有两类,一类是getXXX,表示要激活的意图的类型,如activity,service或者是BroadcastReceiver等。另一类是send,表示立即发送这个意图。
下面是常用的API:
- public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags, Bundle options)
- public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
- public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
- public void send ()
示例:
activity不贴了。
1.PendingIntent单独使用.
下面是activity中的一个button的点击事件所调用的方法.这个方法用于发送一个广播
- /**
- * 单独使用pendingIntent
- * 通过send方法发送intent
- */
- public void pendingIntentFunc()
- {
- Intent intent = new Intent("com.xxx");
- intent.putExtra("info","我是info");
- //其实就跟调用sendBroadcast方法一样
- PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- try
- {
- pi.send();//执行PendingIntent中的意图
- } catch (CanceledException e)
- {
- e.printStackTrace();
- }
- }
- package com.example.receiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import android.widget.Toast;
- public class MyReceiver extends BroadcastReceiver
- {
- private static final String TAG = "MyReceiver";
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Log.i(TAG,"成功收到广播...");
- Toast.makeText(context,"info:"+intent.getStringExtra("info"),0).show();
- }
- }
清单文件中配置receiver:
- <receiver android:name="com.example.receiver.MyReceiver">
- <intent-filter >
- <action android:name="com.xxx"/>
- </intent-filter>
- </receiver>
当点击按钮时,将会发出广播,而且还能接收到intent传来的信息。
logcat打印如下日志:
2.PendingIntent配合Notification使用
下面也是activity中的一个按钮的点击事件触发所执行的函数
- /**
- * 显示一个通知,点击通知将会激活一个服务
- */
- public void sendNotification2()
- {
- NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
- // notification的初始化
- Notification notification = new Notification();
- notification.icon = R.drawable.ic_launcher;
- notification.when = System.currentTimeMillis();
- notification.tickerText = "又来一条新通知";
- notification.flags = notification.FLAG_AUTO_CANCEL;
- // 包装一个PendingIntent,当用户点击通知触发一个意图
- Intent intent = new Intent(this,MyService.class);
- // 点击通知将启动服务,相当于调用了startService方法
- PendingIntent contentIntent = PendingIntent.getService(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
- notification.setLatestEventInfo(this,"标题","点我激活一个服务", contentIntent);
- // 激活通知
- manager.notify(2,notification);//第一个参数代表的是通知的id
- }
定义一个服务:
- package com.example.service;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
- public class MyService extends Service
- {
- private static final String TAG = "MyService";
- @Override
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- @Override
- public void onCreate()
- {
- Log.i(TAG,"服务成功启动...");
- }
- }
清单文件中配置服务:
- <service android:name="com.example.service.MyService"></service>
logcat成功打印日志,服务启动成功:
3.PendingIntent配合SmsManger使用
下面使用SmsManager发送一条短信。
- public void sendSMS()
- {
- SmsManager sm = SmsManager.getDefault();
- PendingIntent sentIntent = PendingIntent.getBroadcast(this,0, new Intent("com.xxx"),PendingIntent.FLAG_UPDATE_CURRENT);
- sm.sendTextMessage("5556", null, "hello world", sentIntent, null);
- }
另外5556也收到了短信: