Intent和PendingIntent 翻译过来都有意图的意思,只是PendingIntent 含有意图不明,表示即将发生或来临的事情。 。
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上,AlarmManager,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
Intent和PendingIntent 都可以用来传递数据,在这里使用Intent来传数据就不说啦,不会的自己查。
一.PendingIntent 传递数据
1.创建PendingIntent 对象 ,创建方式有 参考与: http://blog.csdn.net/hudashi/article/details/7060837
- PendingIntent.getService(context, requestCode, intent, flags) 方法从系统取得一个用于启动一个Service的PendingIntent对象
- PendingIntent.getBroadcast(context, requestCode, intent, flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象
- PendingIntent.getActivity(context, requestCode, intents, flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,
- PendingIntent.FLAG_CANCEL_CURRENT如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
- PendingIntent.FLAG_NO_CREATE如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
- PendingIntent.FLAG_ONE_SHOT该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
- PendingIntent.FLAG_UPDATE_CURRENT如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。
首先创建一个String action = "PendingIntent .send.xxxxxxxxxxxxxxxx";Intent intent = new Intent(action);intent .putExtra("data","数据来啦");
PendingIntent p endingIntent = PendingIntent.getBroadcast(context,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
2. 对PendingIntent 对象 进行操作 我们在这使用闹铃来定时 在5s后会收到广播AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);manager.set(AlarmManager.RTC,
System.currentTimeMillis() +5*1000, pendingIntent);
广播接收:
……
@Override
public void onReceive(Context context, Intent intent) {if(intent.getAction().equals(action)){
String data intent.getIntExtra("data");
Toast.makeText(context,"接收的数据----------- >" + data,0).show();
}
}
5秒后就可以看到效果啦
二 Intent 的使用
1 、在Activity 中使用 开启活动 可以分为两种,显式Intent 和隐式Intent
a. 显式Intent (A,B 是两个Activity ) 活动A开启活动B
Intent intent= new (A.this ,B.class);
startActivity(intent);
这样就可以开启活动
注意:记得在AndroidManifest.xml,配置 否则会报错
……
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".A"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".B" >
</activity>
……
b 隐式Intent (A,B 是两个Activity ) 活动A开启活动B'
在AndroidManifest.xml,添加代码
……
<activity
android:name=".B" >
<intent-filter>
<action android:name="xxx.xxx.xx.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
……
Intent intent= newIntent(xxx.xxx.xx.ACTION_START);
startActivity(intent);
这样也可以开启活动
还有许多隐式Intent 的用法 可以查看http://blog.csdn.net/mylike_45/article/details/41750073