我的应用程序不在Play商店上验证在网络上如果有新版本并下载并启动它.安装完毕后,我想重新启动应用程序,我会用一个BroadcastRecevier带ACTION_PACKAGE_REPLACED.这是代码:
广播:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
ApplicationInfo app = new ApplicationInfo();
if(app.packageName.equals("it.android.downloadapk")){
Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
context.startActivity(LaunchIntent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
表现:
<receiver android:name="it.android.downloadapk.Broadcast">
<intent-filter>
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
<data android:scheme="package" android:path="it.android.downloadapk" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
问题是,当我安装新的apk时,广播似乎没有启动,为什么?
我正在开发一个非公开的Android应用程序,即该应用程序将无法在全球Android Market中使用.该应用程序将安装在有限数量的客户端上,例如使用apk文件.我在SD卡中有一个.apk,我正在尝试从我的应用程序更新我的应用程序.为此,我正在使用意图
我的守则
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/" +"Test.apk")), "application/vnd.android.package-archive");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
注意: 它工作正常,但更新后,应用程序将被关闭.
问题是" 如何防止这种情况? "
我也使用广播接收器重新打开我的应用程序
public class AutoStart extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, ABCActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}else{
Intent i = new Intent(context, XYZActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Run Code Online (Sandbox Code Playgroud)
问题 1: - 无法重新打开活动时
"android.intent.action.PACKAGE_ADDED"
"android.intent.action.PACKAGE_INSTALL"
"android.intent.action.PACKAGE_CHANGED"
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter> …Run Code Online (Sandbox Code Playgroud)