我正在为我的应用编写一个应用更新程序。在我确保我的apk在设备上后,这是我在我试图更新的应用程序中所做的事情:
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
File f = new File(apkLocation);
promptInstall.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
_context.startActivity(promptInstall);
这将启动我的安装程序,其中显示应用程序权限,我可以单击“安装”。但从这里应用程序简单地关闭,我没有得到任何消息(我本以为对话框告诉我安装成功,让我选择按下“关闭”或“打开”)。它只是在没有进一步通知的情况下进入设备的主屏幕。
顺便说一句,当我手动打开应用程序时,它确实会更新。我怎样才能让安装程序完全按照预期进行呢?在意图上有什么需要设置的吗?
在写这篇文章的时候,我想知道发生这种情况的原因是不是因为当前的应用程序在设备上被简单地覆盖了,从而关闭了它,并且在一定程度上没有得到意图的结果,因为它的源被杀死了?
发布于 2013-05-16 06:52:55
您所能做的就是使用意图过滤器注册一个接收器,比如android.intent.action.PACKAGE_INSTALL
或android.intent.action.PACKAGE_REPLACED
,您可以从中重新启动您的应用程序。
<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
和
public class BootService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context,Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
}
}
发布于 2013-07-25 21:31:03
要成功更新,您需要使用URI启动intent,并将其作为新任务指示给更新应用程序。
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK));
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
我的帖子如下:
发布于 2013-05-16 00:12:41
首先,除非您是rooted用户或具有系统权限,否则不能在没有提示的情况下进行安装。我不认为你是在问这个问题,但是你的一段话说得不清楚。
其次,如果安装正在运行的应用程序的更新版本,您看到的行为是意料之中的:应用程序被强制关闭并更新。您不能就地更新。您可以检测到安装何时中止,因为调用安装程序的活动将恢复。
为了更新正在运行的应用程序并使其保持运行,您需要一个单独的进程( app )来监控安装并重新启动您的应用程序。
https://stackoverflow.com/questions/16569647
复制相似问题