我已经尝试了很多在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()并最终回到主屏幕,但正在安装新的更新应用程序。但是,我需要它自己打开它。有什么办法可以做到这一点吗?
与其设置闹钟,不如专门为此目的注册一个清单注册的广播接收器。它可以侦听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安装时,为什么要使用此硬metod?为什么不尝试创建简单的外壳脚本来更新应用程序?然后,您不必设置闹钟和执行其他操作:
脚本(如何传递参数,如何从终端启动应用程序):
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'
...}