您的位置:首页 > 移动开发 > Android开发

Android Apk uninstall install

2015-12-12 16:12 381 查看
android UI install APK 与 监测 ACTION_PACKAGE_ADDED 事件

public class World extends Activity {

 private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();

@Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       registerIntentReceivers() ;

     }

/**

     * Start to install APK file

     * @param file

     */

    private void openFile(File file) {

        Intent intent = new Intent();

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.setAction(Intent.ACTION_VIEW);

 

        intent.setDataAndType(Uri.fromFile(file), APK_MIME_TYPE);

        startActivity(intent);

    }

private void registerIntentReceivers() {

        IntentFilter filter;

      

        filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);

        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);

        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);

        filter.addDataScheme("package");

        registerReceiver(mApplicationsReceiver, filter);

    }

    /**

     * Receives notifications when applications are added/removed.

     */

    private class ApplicationsIntentReceiver extends BroadcastReceiver {

        @Override

        public void onReceive(Context context, Intent intent) {

             {   

                Log.i(TAG,"============================RETURN VALUE ");      

                         if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){   

                             Uri uri = null;

                             uri = intent.getData();

                             Log.i(TAG,"+++++++++++++++++++++++RETURN VALUE "+uri);    

                         }   

                          else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){   

                                 Uri uri = null;

                                  uri = intent.getData();

                                  Log.i(TAG,"----------------------------RETURN VALUE "+uri);      

                         }   
 else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){   

                                 Uri uri = null;

                                  uri = intent.getData();

                                  Log.i(TAG,"----------------------------RETURN VALUE "+uri);      

                         }   

             }

    }

}

【Android】Android 监听apk安装替换卸载广播

首先是要获取应用的安装状态,通过广播的形式

以下是和应用程序相关的Broadcast Action

ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)

ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本

ACTION_PACKAGE_CHANGED 一个已存在的应用程序包已经改变,包括包名

ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)

ACTION_PACKAGE_RESTARTED 用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播)

ACTION_PACKAGE_DATA_CLEARED 用户已经清楚一个包的数据,包括包名(清除包程序不能接收到这个广播)

代码实现 

在AndroidManifest.xml中定义广播

<receiver android:name=".AppInstallReceiver"

            android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.PACKAGE_ADDED" /> 

                <action android:name="android.intent.action.PACKAGE_REPLACED" />

                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />

            </intent-filter>

        </receiver>

这里选用的是

ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)

ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本

ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)

再看AppInstallReceiver 

public class AppInstallReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        PackageManager manager = context.getPackageManager();

        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {

            String packageName = intent.getData().getSchemeSpecificPart();

            Toast.makeText(context, "安装成功"+packageName, Toast.LENGTH_LONG).show();

        }

        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {

            String packageName = intent.getData().getSchemeSpecificPart();

            Toast.makeText(context, "卸载成功"+packageName, Toast.LENGTH_LONG).show();

        }

        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {

            String packageName = intent.getData().getSchemeSpecificPart();

            Toast.makeText(context, "替换成功"+packageName, Toast.LENGTH_LONG).show();

        }

        

    }

}

代码实现比较简单,根据接收到的Action来判断应用程序是安装 卸载还是被替换成其他版本

 

//////////////////////////////////////////////

原创声明 转载请注明

本文出自 Ray-Ray的博客

文章地址 http://www.cnblogs.com/rayray/p/3178403.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android apk