Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android launch applications detail page

I'm working on an application where I list the installed applications with the package manager. I can get the package name of the item clicked, but I'd like to then launch the details screen based on the package. So for instance if Dolphin Browser were selected in the list, you would then see the following image. How can I do this?

enter image description here

Final solution set your target as Gingerbread API level 9 and set your min as API level 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);
}
like image 768
GFlam Avatar asked Jun 04 '11 19:06

GFlam


2 Answers

Here is a fully working app with a ListActivity that lists all installed apps. When you click a package name, it opens the app details.

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

Remember to have 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>

and 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>

in your layout folder. Hope this works :)

like image 167
khellang Avatar answered Oct 13 '22 21:10

khellang


Here is the way to do it, this is a generic answer using build infos from your app. You have to use FLAG_ACTIVITY_NEW_TASK and build the right URI for your app.

  // Build intent that displays the App settings screen.
    Intent intent = new Intent();
                                intent.setAction(
    Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    Uri uri = Uri.fromParts("package",
            BuildConfig.APPLICATION_ID, null);
                                intent.setData(uri);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

You can use it from a snackbar for example.

Snackbar.make(
    findViewById(R.id.activity_layout),
    "snackbar text",
    Snackbar.LENGTH_INDEFINITE)
            .setAction("Settings", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Build intent that displays the App settings screen.
            Intent intent = new Intent();
            intent.setAction(
                    Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package",
                    BuildConfig.APPLICATION_ID, null);
            intent.setData(uri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    })
            .show();
like image 33
Zhar Avatar answered Oct 13 '22 22:10

Zhar