`

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());
    }


mPm.freeStorage(size, pi.getIntentSender());

以下是在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");
                    }
                }
            }
        });
    }


opFinishedIntent.send((retCode >= 0) ? 1 : 0);在PackageInstaller定义的PendingIntent,在PackageManagerService里面来触发。



分享到:
评论
4 楼 追求幸福 2011-07-06  
h286271819 写道
怎么实现对第三方应用程序的缓存清理啊????

不是很懂你的问题哦!
3 楼 h286271819 2011-07-06  
怎么实现对第三方应用程序的缓存清理啊????
2 楼 追求幸福 2010-11-09  
rmn190 写道
下面没有啦?就这么点干巴巴的代码, 哇呀呀....

朋友,我已经增加了一点点注解,希望对你有帮助。有时候,有些东西是"只可意会不可言传滴"
1 楼 rmn190 2010-11-05  
下面没有啦?就这么点干巴巴的代码, 哇呀呀....

相关推荐

Global site tag (gtag.js) - Google Analytics