2010-12-12 148 views

回答

8

在Android 2.3的,你可以在一個ACTION_APPLICATION_DETAILS_SETTINGSIntent使用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 /意圖(過濾器),因此可能會在未來發生變化。

+0

我不會推薦後者。如果惡意應用程序使用該軟件包會怎麼樣:com.android.settings? – 2013-09-18 10:39:57

+0

@PaoloRovelli我寫了「不一定推薦」,但出於不同的原因。我認爲你不能避免你提到的危險;如果兩個應用程序使用相同的意圖(字符串),則用戶將始終獲得一個對話框以選擇要使用的應用程序(這會降低風險)。但是,請注意,第一種方法中的參數也不過是一個字符串:http://developer.android.com/reference/android/provider/Settings.html#ACTION_APPLICATION_SETTINGS - 因此無法保護惡意應用程序如果它使用com.android.settings包名稱。 – 2013-09-18 12:33:15

76

在2.2及更低版本中,沒有可以訪問的公共API。但是,您仍然可以像ManageApplications一樣啓動InstalledApp詳細信息活動。看到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); 
} 
+0

非常感謝你!這節省了我很多時間。如果評論「2.3以上」改爲「2.3以上」? – kristianlm 2011-09-14 18:31:04

+0

這對任何已安裝的應用程序來說都是完美的。但是,如果我嘗試打開任何系統應用程序的設置,會出現空白屏幕。 – 2012-01-10 23:42:41

+1

我們還需要添加intent.setFlags(意圖。FLAG_ACTIVITY_NEW_TASK);在這兩個聲明。即if(){... here ...} else {... here ...} – 2013-12-31 09:26:31

52

API等級9(安卓2.3),你就可以開始與意向android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS。因此:

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

} 
14

老問題,提高了答案:

/** 
* <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; 
} 
9

我用保羅的解決方案,以打開SDK應用程序詳細信息設置23+當用戶拒絕在過去的許可請求和在權限請求系統對話框中選擇「不要再詢問」選項。

但在這種情況下,我直接使用getPackageName()方法。

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