2011-06-04 113 views
14

我正在使用程序包管理器列出已安裝應用程序的應用程序。我可以獲取點擊項目的包名稱,但是我想根據包來啓動細節屏幕。因此,例如,如果海豚瀏覽器在列表中被選中,您會看到下面的圖像。我怎樣才能做到這一點?Android啓動應用程序詳細信息頁面

enter image description here

最終解決方案設定目標爲Gingerbread API 9級,並設置你分鐘爲API級別7

final int apiLevel = Build.VERSION.SDK_INT; 
Intent intent = new Intent(); 
if (apiLevel >= 9) { 
    //TODO get working on gb 
    //Toast.makeText(SDMove.this, "Gingerbread Not Currently Supported", Toast.LENGTH_LONG).show(); 
    startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, 
          Uri.parse("package:" + pli.pkg.packageName))); 
} else { 
    final String appPkgName = (apiLevel == 8 ? "pkg" : "com.android.settings.ApplicationPkgName"); 
    intent.setAction(Intent.ACTION_VIEW); 
    intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); 
    intent.putExtra(appPkgName, pli.pkg.packageName); 
    startActivity(intent); 
} 

回答

10

這是一個全面運行的應用程序,其中列出了所有已安裝的應用程序,其中ListActivity。當您單擊包名稱時,會打開應用程序詳細信息。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Intent for getting installed apps. 
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

    // Get installed apps 
    List<ResolveInfo> appList = this.getPackageManager().queryIntentActivities(mainIntent, 0); 

    // Make new list for package names and fill the list. 
    List<String> packageNameList = new ArrayList<String>(); 
    for (ResolveInfo resolveInfo : appList) { 
     packageNameList.add(resolveInfo.activityInfo.packageName); 
    } 

    // Set the list adapter. 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.simple, packageNameList)); 
} 

public void onListItemClick(ListView l, View v, int position, long id) 
{ 
    // Get the TextView that was clicked. 
    TextView view = (TextView)v; 

    // Get the text from the TextView. 
    String packageName = (String)view.getText(); 

    // Open AppDetails for the selected package. 
    showInstalledAppDetails(packageName); 
} 

public void showInstalledAppDetails(String packageName) { 
    final int apiLevel = Build.VERSION.SDK_INT; 
    Intent intent = new Intent(); 

    if (apiLevel >= 9) { 
     intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
     intent.setData(Uri.parse("package:" + packageName)); 
    } else { 
     final String appPkgName = (apiLevel == 8 ? "pkg" : "com.android.settings.ApplicationPkgName"); 

     intent.setAction(Intent.ACTION_VIEW); 
     intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); 
     intent.putExtra(appPkgName, packageName); 
    } 

    // Start Activity 
    startActivity(intent); 
} 

記得有main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="No apps installed"/> 
</LinearLayout> 

simple.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</TextView> 
佈局文件夾

。希望這個作品:)

+0

是否有明確的許可或爲此?我不斷收到與我嘗試的任何東西關閉的力量,我只是試着將你的方法設置爲桌面時鐘等簡單的東西。我把你的方法然後運行showInstalledAppDetails(「com.android.deskclock」);但無論我嘗試它的力量關閉我正在運行項目精英gb根扎d1,但應用程序我已設置爲2.2作爲我的最小sdk,但不知道爲什麼我每次強制關閉 – GFlam 2011-06-04 23:23:47

+0

我得到它在我的2.2模擬器上運行,但是當我嘗試「com.android.deskclock」時,「檢索包時發生異常:com.android.deskclock」。嘗試另一個應用,例如「com.android.settings」,在模擬器中工作。包名是正確的非常重要:)如果你沒有得到它的工作,你可以從LogCat發佈堆棧跟蹤。 – khellang 2011-06-05 01:56:56

+0

是剛做了一個新的項目與2.3作爲目標這種方法部隊關閉,但下面的代碼與你一樣工作2.3,但我需要它在2.2,因爲我正在編寫這個應用程序的2.2 ROM。反正張貼在第二的logcat的沒有看到真的有什麼,但也許你會 – GFlam 2011-06-05 02:10:38

9

試試這個:

startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:your.package.here"))); 

和替換「包:your.package.here「與真正的包你想查看...

+0

這只是2.3 ... – khellang 2011-06-04 20:09:47

+0

工作在2.2應用程序的任何東西嗎? – GFlam 2011-06-04 20:18:44

+0

這個答案好多了:) – 2013-08-26 08:12:11

相關問題