繁体   English   中英

如何以编程方式静默更新 apk 并重新启动应用程序?

[英]How do I programmatically update apk silently and restart the app?

我已经尝试了 StackOverFlow 和其他网站中给出的许多方法,但它并没有真正起作用。

我的问题是我有这个应用程序需要更新,更新后,它应该自动重新打开同一个应用程序(更新)。

这是我的代码:

private synchronized void runRootUpdate() {    
    // Install Updated APK
    String command = "pm install -r " + downloadPath + apkFile;
    Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", command});
    int test = proc.waitFor(); // Error is here.

    if (proc.exitValue() == 0) {
        // Successfully installed updated app
        doRestart()
    } else {
        // Fail
        throw new Exception("Root installation failed");
    }
}

private void doRestart() {
    Intent mStartActivity = new Intent(context, MainActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}

看看我的“错误在这里”。 我的旧应用程序将安装一个新的更新应用程序并自行终止,因此没有 proc.waitFor() 并最终返回主屏幕,但正在安装新的更新应用程序。 但是,我需要它来重新启动它。 有什么办法可以做到吗?

而不是设置警报,而是专门为此目的注册了一个Manifest注册的BroadcastReceiver。 它可以监听android.intent.action.PACKAGE_REPLACED (注意:此常量中缺少单词action)或者android.intent.action.MY_PACKAGE_REPLACED

<receiver android:name="...">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
        <data android:scheme="package" android:path="com.your.package" /> 
    </intent-filter>
</receiver>

重新安装后,您的接收器将收到此消息,您将能够启动活动

更新应用程序涉及杀死其所有进程。 您应该启动pm命令之前设置警报(延迟超过100毫秒)

首先,您需要知道存储.apk文件的位置,因为在升级应用程序时这很重要。

要以静默方式升级和启动应用程序,您需要执行以下步骤。

确保在升级应用程序时在SD卡中有最新的.apk文件。如果有,则通过如下的意图启动它以进行升级

File file = new File(Environment.getExternalStorageDirectory(), "app-debug.apk"); // mention apk file path here
                if (file.exists()) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    startActivity(intent);
                } else {
                    Toast.makeText(UpdateActivity.this, "file not exist", Toast.LENGTH_SHORT).show();
                }

上面的代码将使用android的默认行为升级您的应用程序。

创建一个广播接收器,它将知道何时执行升级

    <receiver android:name=".receiver.OnUpgradeReceiver">
                <intent-filter>
// Note: This intent can is available starting from API 12
                    <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                </intent-filter>
            </receiver>

如果你使用api> 12使用

<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                    </intent-filter>

如果你有api <= 12使用

  <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <data android:scheme="package" android:path="your.app.package" />

创建广播接收器,以便在更新后启动广播并启动所需的活动

示例:

public class OnUpgradeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
       // launch your fav activity from here.
    }
}

以上广播接收器可帮助您静默启动应用程序。

希望这些能帮到你。

嗯......当你使用root安装时,为什么要使用这个硬盘 为什么不尝试创建简单的shell脚本来更新应用程序? 然后你不必设置闹钟和做其他事情:

脚本( 如何传递参数如何从终端启动应用程序 ):

am kill com.myapp.package
pm install -r $1
am start -n packageName/pkgName.ActivityName

应用代码:

String command = "sh script.sh " + downloadPath + apkFile;
Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", command});

使用Play 核心库应用内更新来实现此目的

dependencies {
implementation 'com.google.android.play:core:1.5.0'
...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM