如何在Android中以编程方式启动软件更新?

时间:2015-03-16 06:23:42

标签: android performance android-layout android-intent android-activity

我需要在点击通知时启动更新Android操作系统 我正在使用此代码:

Intent callGPSSettingIntent =new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS);
        startActivityForResult(callGPSSettingIntent,1);

但它只将用户移动到“关于设备”部分,然后用户可以看到软件更新选项。

我的要求是再深一步,当用户点击通知时,软件更新会自动启动。

1 个答案:

答案 0 :(得分:1)

从活动中调用此函数:

openSettingsPage(this, "android.settings.SYSTEM_UPDATE_SETTINGS");

public void openSettingsPage(Context context, String setting) {
    Intent intent = new Intent(setting, null);

    final boolean isAndroidJellyBean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
        && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;

    if (isAndroidJellyBean || !areSettingsAvailable(context, intent)) {
        intent = new Intent(Settings.ACTION_SETTINGS);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

public Boolean areSettingsAvailable(Context context, Intent intent) {
    List<ResolveInfo>
        activities = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return activities != null && activities.size() > 0;
}