浅谈 Intent、PendingIntent

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对象,

  1. PendingIntent.FLAG_CANCEL_CURRENT如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
  2. PendingIntent.FLAG_NO_CREATE如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
  3. PendingIntent.FLAG_ONE_SHOT该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException
  4. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值