【安卓笔记】PendingIntent
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 ()
/** * 单独使用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(); } }定义一个BroadcastReceiver用于接收广播:
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>
/** * 显示一个通知,点击通知将会激活一个服务 */ 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>当点击任务栏通知即开启服务:
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); }PendingIntent作为senTextMessage的参数被传递进来,作为短信发送成功的回执,此时PendingIntent会发送一个广播。还是以上面例子的广播接收者接收广播,可以看到logcat打印了日志:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。