【问题标题】:How can I start android application info screen programmatically?如何以编程方式启动 android 应用程序信息屏幕?
【发布时间】:2010-12-12 12:06:32
【问题描述】:

是否可以从另一个应用程序启动“应用程序信息”屏幕(即MenuSettingsApplicationsManage Applications→选择任何应用程序)?

【问题讨论】:

  • 有没有办法打开特定应用的DATA USAGE详细信息?

标签: android


【解决方案1】:

在 2.2 及更低版本中,没有您可以访问的公共 API。但是您仍然可以像 ManageApplications 一样启动 InstalledAppDetails 活动。见here

 // utility method used to start sub activity
 private void startApplicationDetailsActivity() {
     // Create intent to start new activity
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setClass(this, InstalledAppDetails.class);
     intent.putExtra(APP_PKG_NAME, mCurrentPkgName);
     // start new activity to display extended information
     startActivityForResult(intent, INSTALLED_APP_DETAILS);
 }

结论:您可以像我写的这样启动“应用程序信息”屏幕:

private static final String SCHEME = "package";

private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";

private static final String APP_PKG_NAME_22 = "pkg";

private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";

private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";

public static void showInstalledAppDetails(Context context, String packageName) {
    Intent intent = new Intent();
    final int apiLevel = Build.VERSION.SDK_INT;
    if (apiLevel >= 9) { // above 2.3
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts(SCHEME, packageName, null);
        intent.setData(uri);
    } else { // below 2.3
        final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
                : APP_PKG_NAME_21);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setClassName(APP_DETAILS_PACKAGE_NAME,
                APP_DETAILS_CLASS_NAME);
        intent.putExtra(appPkgName, packageName);
    }
    context.startActivity(intent);
}

【讨论】:

  • 非常感谢!这为我节省了很多时间。注释“2.3以上”是否应该改为“2.3及以上”?
  • 这适用于任何已安装的应用程序。但是,如果我尝试打开任何系统应用程序的设置,则会出现一个空白屏幕。
  • 另外我们需要添加intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);在这两种说法中。即 if(){...here...} else {...here...}
  • 太棒了! +100 米/
  • 是的,它正在工作。真棒!
【解决方案2】:

API 级别 9 (Android 2.3) 开始,您可以使用 android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS 启动 Intent。因此:

packageName = "your.package.name.here"

try {
    //Open the specific App Info page:
    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.parse("package:" + packageName));
    startActivity(intent);

} catch ( ActivityNotFoundException e ) {
    //e.printStackTrace();

    //Open the generic Apps page:
    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
    startActivity(intent);

}

【讨论】:

    【解决方案3】:

    当用户过去拒绝权限请求并在权限请求系统对话框中选择“不再询问”选项时,我使用 Paolo 解决方案打开 SDK 23+ 中的应用程序详细信息设置。

    但在这种情况下,我直接使用了getPackageName() 方法。

    Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivity(intent);
    

    【讨论】:

      【解决方案4】:

      老问题,改进答案:

      /**
       * <p>Intent to show an applications details page in (Settings) com.android.settings</p>
       * 
       * @param context       The context associated to the application
       * @param packageName   The package name of the application
       * @return the intent to open the application info screen.
       */
      public static Intent newAppDetailsIntent(Context context, String packageName) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
              Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              intent.setData(Uri.parse("package:" + packageName));
              return intent;
          } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.FROYO) {
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              intent.setClassName("com.android.settings",
                      "com.android.settings.InstalledAppDetails");
              intent.putExtra("pkg", packageName);
              return intent;
          }
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setClassName("com.android.settings",
                  "com.android.settings.InstalledAppDetails");
          intent.putExtra("com.android.settings.ApplicationPkgName", packageName);
          return intent;
      }
      

      【讨论】:

        【解决方案5】:

        在 Android 2.3 中,您可以在 ACTION_APPLICATION_DETAILS_SETTINGS Intent 上使用 startActivity() 和适当的 Uri,以调出应用的“管理”屏幕。但是,这对于 Android 2.3 来说是新的——我不知道在以前的 Android 版本中有什么方法可以做到这一点。

        【讨论】:

          【解决方案6】:
          startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS));
          

          将带您进入设置/应用程序列表。 如果你想打开一个特定的应用程序,我认为在 2.2 及以下没有办法,所以你需要走一种(不一定建议,因为非官方的)方式:

          final Intent i = new Intent("android.intent.action.VIEW");                
          i.setComponent(new ComponentName("com.android.settings","com.android.settings.InstalledAppDetails"));
          i.putExtra(...); // need to figure out the correct extra, probably app package name
          startActivity(i);
          

          但请注意,不建议这样做,因为它不是官方 API/意图(过滤器),因此将来可能会发生变化。

          【讨论】:

          • 我不会推荐后者。如果恶意应用使用包:com.android.settings 怎么办?
          • @PaoloRovelli 我写了“不一定推荐”,但出于不同的原因。我认为您无法避免您提到的危险;如果两个应用程序使用相同的意图(字符串),那么用户将始终得到一个对话框来选择要使用的应用程序(这会降低风险)。但还要注意,第一种方法中的参数也不过是一个字符串:developer.android.com/reference/android/provider/… - 因此,如果恶意应用程序使用 com.android.settings 包名称,也无法保护它。
          猜你喜欢
          • 2020-04-29
          • 1970-01-01
          • 2023-01-12
          • 2012-03-04
          • 2018-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-30
          相关资源
          最近更新 更多