android 调用系统设置界面

在编写android应用程序时,如果需要调用系统原生的管理应用程序界面呢?本人在一个项目中遇到过,本人没有发现这方面现成的intent,不过通过看源代码实现了。

android源代码application_settings.xml

  1. <PreferenceScreen  

  2.           android:title="@string/manageapplications_settings_title"  

  3.           android:summary="@string/manageapplications_settings_summary">  

  4.       <intent android:action="android.intent.action.MAIN"  

  5.               android:targetPackage="com.android.settings"  

  6.               android:targetClass="com.android.settings.ManageApplications" />  

  7.   </PreferenceScreen>  


熟悉PreferenceScreen的朋友应该知道如何做了,如果不熟悉的朋友,文章最后贴出一篇文章,大家参考一下就知道了,根据xml的描述,我们可以用如下代码调用系统原生的管理应用程序界面

  1. Intent intent =  new Intent();  

  2. intent.setAction("android.intent.action.MAIN");  

  3. intent.setClassName("com.android.settings", "com.android.settings.ManageApplications");  

  4. startActivity(intent);  

如果要进入某个软件详细设置界面 可以如下操作:  

“Android系统设置->应用程序->管理应用程序”列表下,列出了系统已安装的应用程序。选择其中一个程序,则进入“应用程序信息(Application Info)”界面。这个界面显示了程序名称、版本、存储、权限等信息,并有卸载、停止、清除缓存等按钮,可谓功能不少。如果在编写相关程序时(比如任务管理器)可以调用这个面板,自然提供了很大的方便。那么如何实现呢?

在最新的Android SDK 2.3(API Level 9)中,提供了这样的接口。在文档路径

docs/reference/android/provider/Settings.html#ACTION_APPLICATION_DETAILS_SETTINGS

下,有这样的描述:

public static final String ACTION_APPLICATION_DETAILS_SETTINGS    Since: API Level 9

Activity Action: Show screen of details about a particular application.
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
Input: The Intent's data URI specifies the application package name to be shown, with the "package" scheme. That is "package:com.my.app".
Output: Nothing.
Constant Value: "android.settings.APPLICATION_DETAILS_SETTINGS"

就是说,我们只要以android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS作为Action;package:应用程序的包名作为URI,就可以用startActivity启动应用程序信息界面了。代码如下:

  1. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  

  2. Uri uri = Uri.fromParts(SCHEME, packageName, null);  

  3. intent.setData(uri);  

  4. startActivity(intent);  

但是,在Android 2.3之前的版本,并没有公开相关的接口。
通过查看系统设置platform/packages/apps/Settings.git程序的源码,可以发现应用程序信息界面为InstalledAppDetails。
这里(2.1)还有这里(2.2),我们可以分别看到Android2.1Android2.2的应用管理程序(ManageApplications.java)是如何启动InstalledAppDetails的。
  1. // utility method used to start sub activity  

  2. private void startApplicationDetailsActivity() {  

  3.    // Create intent to start new activity  

  4.    Intent intent = new Intent(Intent.ACTION_VIEW);  

  5.    intent.setClass(this, InstalledAppDetails.class);  

  6.    intent.putExtra(APP_PKG_NAME, mCurrentPkgName);  

  7.    // start new activity to display extended information  

  8.    startActivityForResult(intent, INSTALLED_APP_DETAILS);  

  9. }  

但是常量APP_PKG_NAME的定义并不相同。
2.2中定义为"pkg",2.1中定义为"com.android.settings.ApplicationPkgName"
那么,对于2.1及以下版本,我们可以这样调用InstalledAppDetails:
  1. Intent i = new Intent(Intent.ACTION_VIEW);                  

  2. i.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");  

  3. i.putExtra("com.android.settings.ApplicationPkgName", packageName);  

  4. startActivity(i);  

对于2.2,只需替换上面putExtra的第一个参数为"pkg"
综上,通用的调用“应用程序信息”的代码如下:
  1. private static final String SCHEME = "package";  

  2. /**

  3. * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)

  4. */  

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

  6. /**

  7. * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)

  8. */  

  9. private static final String APP_PKG_NAME_22 = "pkg";  

  10. /**

  11. * InstalledAppDetails所在包名

  12. */  

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

  14. /**

  15. * InstalledAppDetails类名

  16. */  

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

  18. /**

  19. * 调用系统InstalledAppDetails界面显示已安装应用程序的详细信息。 对于Android 2.3(Api Level

  20. * 9)以上,使用SDK提供的接口; 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)。

  21. *

  22. * @param context

  23. *

  24. * @param packageName

  25. *            应用程序的包名

  26. */  

  27. public static void showInstalledAppDetails(Context context, String packageName) {  

  28.    Intent intent = new Intent();  

  29.    final int apiLevel = Build.VERSION.SDK_INT;  

  30.    if (apiLevel >= 9) { // 2.3(ApiLevel 9)以上,使用SDK提供的接口  

  31.        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  

  32.        Uri uri = Uri.fromParts(SCHEME, packageName, null);  

  33.        intent.setData(uri);  

  34.    } else { // 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)  

  35.        // 2.2和2.1中,InstalledAppDetails使用的APP_PKG_NAME不同。  

  36.        final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22  

  37.                : APP_PKG_NAME_21);  

  38.        intent.setAction(Intent.ACTION_VIEW);  

  39.        intent.setClassName(APP_DETAILS_PACKAGE_NAME,  

  40.                APP_DETAILS_CLASS_NAME);  

  41.        intent.putExtra(appPkgName, packageName);  

  42.    }  

  43.    context.startActivity(intent);  

  44. }