如何以编程方式更新和启动应用程序?

时间:2018-09-09 14:17:40

标签: android

我想更新现有的android应用并在安装后启动最新的一个。 为此,我创建了一个广泛的广播接收器。但它不起作用。正在安装新应用程序。但未在onReceive方法代码中执行。 这是我放在清单文件中的代码

<receiver android:name=".AppInstalledUninstalledReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED"/>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
               <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                <data android:scheme="package" android:path="com.panaceata.mdm" />
                <action android:name="android.intent.action.PACKAGES_UNSUSPENDED"/>
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <data android:scheme="package"/>
               </intent-filter>
        </receiver>

Java:

public class AppInstalledUninstalledReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(Constants.LOGTAG, "Inside application installed receiver");
            String appPackageName = intent.getData().getEncodedSchemeSpecificPart();
            App detectedApp = DeviceAdministrator.appStorage().getAppByPackageName(appPackageName);
            if (intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_ADDED")) {
                if (detectedApp == null) {
                    new UnInstallFile().unInstallApk(appPackageName);
                } else {
                    Log.i(Constants.LOGTAG, "New App has been installed: " + appPackageName);
                    DeviceAdministrator.appManager().loadApps();
                    JobsHandler.updateJobStatusByPackageName(0, appPackageName, 2);
                }

            } else if (intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_REMOVED")) {
                Log.i(Constants.LOGTAG, "An app has been uninstalled: " + appPackageName);
                if (detectedApp != null) {
                    DeviceAdministrator.appManager().loadApps();
                    JobsHandler.updateJobStatusByPackageName(1, appPackageName, 2);
                }
            }
            else if (intent.getAction().equalsIgnoreCase("android.intent.action.MY_PACKAGE_REPLACED")){
                context.startActivity(new Intent(context,MainActivity.class));
                JobsHandler.updateJobStatusByPackageName(1, appPackageName, 2);
            }
        }

我要在安装后启动它,然后将这个新文件的大小加倍后再安装。如果我安装了另一个应用程序,则onReceive方法可能会执行。

0 个答案:

没有答案