總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年2月29日 星期三

偵測APK install / uninstall 所發出的Broadcast事件

當有AP被install / uninstall時,
系統會發出Broadcast :ACTION_PACKAGE_ADDED / ACTION_PACKAGE_REMOVED,
假設我們在APP List的畫面中,
希望能"即時"的在AP有新增/移除時變更於Activity,
就需要建立BroadcastReceiver

=======================方法一:不用新增class也不用在Manifest.xml中宣告=======================
@Override
protected void onResume()
{
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);     //只能接收用adb install的安裝事件
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);//只能接收用adb uninstall的反安裝事件
    filter.addDataScheme("package");
    mIntentReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {               
            final String action = intent.getAction();
            if (Intent.ACTION_PACKAGE_ADDED.equals(action))
            {               

                Uri data = intent.getData();
                String pkgName = data.getEncodedSchemeSpecificPart();
                //pkgName被安裝AP的package name, 在存入SQLite時必須要的資訊

                queryAPInfo();
                Toast.makeText(context, "ACTION_PACKAGE_ADDED", Toast.LENGTH_SHORT).show();
            }
            else if (Intent.ACTION_PACKAGE_REMOVED.equals(action))
           {
                queryAPInfo();
                Toast.makeText(context, "ACTION_PACKAGE_REMOVED", Toast.LENGTH_SHORT).show();
            }
        }
    };       
registerReceiver(mIntentReceiver, filter);

super.onResume();
}

@Override
protected void onPause()
{
    context.unregisterReceiver(mIntentReceiver);
    super.onPause();
}

//改一下queryAPInfo()
//此function所做的: 重新查一遍package的資料, 並setAdapter()更新畫面
public void ArrayList<String> queryAPInfo()
{
    for(int i = 0 ; i <allappPagInfo.size() ; i ++)
    {              
        list_apPag = new ArrayList<String>();
        list_apName = new ArrayList<String>(); 
        PackageInfo appPI = allappPagInfo.get(i); //PackageInfo{2ad3d2e0 com.android.launcher}
        String apPag = appPI.packageName;         //com.android.launcher
        list_apPag.add(apPag);
        String apName = appPI.applicationInfo.loadLabel(packageManager).toString(); //Launcher
        list_apName.add(apName);   
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list_apName);
    GridView gridView1 = (GridView) findViewById (R.id.gridView1);
    gridView1.setAdapter(adapter);
}

 =======================方法二:新增class且要在Manifest.xml中宣告=======================
1. adb uninstall
2. User在Device上操作解除安裝
都可以偵測到 
./AndroidManifest.xml
<receiver android:name=".PackageReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
     </intent-filter>
</receiver>

./src/com.tsots.AppManager/Activity.java
package com.tsots.AppManager;
public class Activity extends Activity
{
    protected void onCreate(Bundle icicle) 
    {
        .....
    }

    @Override
    protected void onResume()
    {
        PackageReceiver BRC = new PackageReceiver();
        IntentFilter mFilter1, mFilter2;
        //過濾PACKAGE_ADDED與PACKAGE_REMOVED事件
        mFilter1 = new IntentFilter(BRC.addpk_ACTION);
        mFilter2 = new IntentFilter(
BRC.removepk_ACTION);
        mFilter1.addDataScheme("package");
        mFilter2.addDataScheme("package");
        //註冊Receiver     
        registerReceiver(BRC, mFilter1);
        registerReceiver(BRC, mFilter2);

        super.onResume();
    }

    public class PackageReceiver extends BroadcastReceiver
    {
        private static final String addpk_ACTION = "android.intent.action.PACKAGE_ADDED";
        private static final String removepk_ACTION = "android.intent.action.PACKAGE_REMOVED";       
 
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if (intent.getAction().equals(addpk_ACTION))
            {       
                Uri data = intent.getData();
                String pkgName = data.getEncodedSchemeSpecificPart();

                //pkgName為package名稱
                Intent iSend = new Intent(addpk_ACTION);

                //送出廣播 
                context.sendBroadcast(iSend);
            }         
            else if (intent.getAction().equals(removepk_ACTION))
            {               
                Uri data = intent.getData();
                String pkgName = data.getEncodedSchemeSpecificPart();

                //pkgName為package名稱 
                Intent iSend = new Intent(removepk_ACTION);

                //送出廣播
                context.sendBroadcast(iSend);
            }
        }
    }

}

沒有留言:

張貼留言