PendingIntent与Intent的区别的理解

以前在学习AlarmManager里面会遇到PendingIntent,我就很迷惑,PendingIntent和Intent的区别是什么呢?我在网上搜索了,但看了许多人的解释还是不理解,今天有看到了PendingIntent的另一种用法,所以我重新理解一遍PendingIntent.不知道是否理解正确,只是把自己的理解写出来,大家一起共同探讨。相信大家都知道Intent是你的意图,比如你想启动一个Activity,就会通过Intent来描述启动这个Activity的某些特点,让系统找到这个Activity来启动,而不是启动别的Activity.StartActivity(intent)就会立即启动这个Activity.而PendingIntent呢?Penging中文意思就是:待定,将来发生或来临。PendingIntent的就的意思就是不是像Intent那样立即发生,而是在合适的时候才会去触发对应的Intent.有人说这个intent不是你的ap来触发而是交给别的ap来触发。你可以看以下的code来理解:
这是在PackageInstaller的代码
private class ClearCacheReceiver extends BroadcastReceiver {
public static final String INTENT_CLEAR_CACHE =
"com.android.packageinstaller.CLEAR_CACHE";
@Override
public void onReceive(Context context, Intent intent) {
Message msg = mHandler.obtainMessage(FREE_SPACE);
msg.arg1 = (getResultCode() ==1) ? SUCCEEDED : FAILED;
mHandler.sendMessage(msg);
}
}



private void checkOutOfSpace(long size) {
if(localLOGV) Log.i(TAG, "Checking for "+size+" number of bytes");
if (mClearCacheReceiver == null) {
mClearCacheReceiver = new ClearCacheReceiver();
}
registerReceiver(mClearCacheReceiver,
new IntentFilter(ClearCacheReceiver.INTENT_CLEAR_CACHE));
PendingIntent pi = PendingIntent.getBroadcast(this,
0, new Intent(ClearCacheReceiver.INTENT_CLEAR_CACHE), 0);
mPm.freeStorage(size, pi.getIntentSender());
}


[color=red]mPm.freeStorage(size, pi.getIntentSender());[/color]

以下是在PackageManagerService的:

public void freeStorage(final long freeStorageSize, final PendingIntent opFinishedIntent) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.CLEAR_APP_CACHE, null);
// Queue up an async operation since clearing cache may take a little while.
mHandler.post(new Runnable() {
public void run() {
mHandler.removeCallbacks(this);
int retCode = -1;
if (mInstaller != null) {
retCode = mInstaller.freeCache(freeStorageSize);
if (retCode < 0) {
Log.w(TAG, "Couldn't clear application caches");
}
}
if(opFinishedIntent != null) {
try {
// Callback via pending intent
opFinishedIntent.send((retCode >= 0) ? 1 : 0);
} catch (CanceledException e1) {
Log.i(TAG, "Failed to send pending intent");
}
}
}
});
}


[color=red]opFinishedIntent.send((retCode >= 0) ? 1 : 0);[/color]在PackageInstaller定义的PendingIntent,在PackageManagerService里面来触发。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值