gpt4 book ai didi

android 以编程方式更新 apk 并查看安装结果

转载 作者:IT老高 更新时间:2023-10-28 23:33:07 29 4
gpt4 key购买 nike

我正在为我的应用编写应用更新程序。在我确定设备上有我的 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);

这会启动我的安装程序,它会显示应用程序权限,并且我可以单击“安装”。但是从这里应用程序只是关闭,我没有收到任何消息(我希望对话框告诉我安装成功,让我可以选择按“关闭”或“打开”)。它只是进入设备的主屏幕,无需另行通知。

附带说明,当我手动打开该应用程序时,它确实已更新。如何使安装程序按预期进行?有什么要设置的 Intent 吗?

在写这篇文章时,我想知道发生这种情况的原因是否是当前应用程序在设备上被简单地覆盖从而关闭了它,并且由于它的源被杀死而在某种程度上没有得到 Intent 的结果?

最佳答案

您所能做的就是用 android.intent.action.PACKAGE_INSTALLandroid.intent.action.PACKAGE_REPLACED 等 Intent 过滤器注册一个接收器,您可以从中重新启动你的申请又回来了。

<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);
}
}
}

关于android 以编程方式更新 apk 并查看安装结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16569647/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com