如何避免在 Android 程序化安装中出现多个 apk 安装提示?
How do I avoid multiple apk install promps on an Android programmatic install?
我们的 Android 应用程序在后台每 5 分钟自动检查一次更新,并从我们的下载服务器下载最新的 .apk 文件。
然后使用以下方法启动安装:
public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
这会提示最终用户(使用标准 Android OS 应用程序安装提示)安装或取消应用程序 apk。
如果我们只有一个应用程序需要更新,则 Android 安装提示只会出现一次,无论上述代码在该应用程序 运行s 中出现多少次。
我们遇到的问题是,如果用户长时间离开他的 Android 设备并且他的多个应用程序需要同时自动更新,则此代码是 运行每个应用程序需要 5 分钟,但现在第二个尝试安装的应用程序会出现多个 Android 安装提示。
示例 1:只有应用程序 X 获得更新,用户离开它 15 分钟,并且只出现一个应用程序 X 的安装提示。
示例 2:应用程序 X 和 Y 都获得更新,用户离开它 15 分钟,应用程序 X 出现 1 个安装提示,应用程序 Y 出现 3 个安装提示
任何想法可能导致示例 2 中的问题?
谢谢
您的服务器告诉您最新的 APK。将此与您的下载区域中的比较。如果您已经下载了最新版本,则无需再次下载。
此外,当您通过 Intent
开始安装时,请记住将版本 ID 和 date/time 写入共享首选项。在 X hours/days 之前不要尝试再次安装相同的版本。
通过让我们的后台服务调用自定义 activity:
,我成功地使其在没有重复项的情况下正常工作
public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(context, InstallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
然后我们的自定义 activity 执行后台服务过去执行的操作:
/**
* We use an activity to kick off the installer activity in order to avoid issues that arise when kicking off the apk installer from a background services
* for multiple applications at the same time.
* */
public class InstallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
this.startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
finish();
}
}
我们的 Android 应用程序在后台每 5 分钟自动检查一次更新,并从我们的下载服务器下载最新的 .apk 文件。
然后使用以下方法启动安装:
public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
这会提示最终用户(使用标准 Android OS 应用程序安装提示)安装或取消应用程序 apk。
如果我们只有一个应用程序需要更新,则 Android 安装提示只会出现一次,无论上述代码在该应用程序 运行s 中出现多少次。
我们遇到的问题是,如果用户长时间离开他的 Android 设备并且他的多个应用程序需要同时自动更新,则此代码是 运行每个应用程序需要 5 分钟,但现在第二个尝试安装的应用程序会出现多个 Android 安装提示。
示例 1:只有应用程序 X 获得更新,用户离开它 15 分钟,并且只出现一个应用程序 X 的安装提示。
示例 2:应用程序 X 和 Y 都获得更新,用户离开它 15 分钟,应用程序 X 出现 1 个安装提示,应用程序 Y 出现 3 个安装提示
任何想法可能导致示例 2 中的问题?
谢谢
您的服务器告诉您最新的 APK。将此与您的下载区域中的比较。如果您已经下载了最新版本,则无需再次下载。
此外,当您通过 Intent
开始安装时,请记住将版本 ID 和 date/time 写入共享首选项。在 X hours/days 之前不要尝试再次安装相同的版本。
通过让我们的后台服务调用自定义 activity:
,我成功地使其在没有重复项的情况下正常工作public static void installDownloadedApplication(Context context) {
Intent intent = new Intent(context, InstallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
然后我们的自定义 activity 执行后台服务过去执行的操作:
/**
* We use an activity to kick off the installer activity in order to avoid issues that arise when kicking off the apk installer from a background services
* for multiple applications at the same time.
* */
public class InstallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, Constants.APPLICATION_CODE+".apk");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
this.startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
finish();
}
}