get name of the app the user chose from app list (android)

拟墨画扇 提交于 2019-12-01 13:02:12

问题


making an android app, i have this intent setup to let the user choose from a list of all apps and it works ok.

            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);            
            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
            startActivityForResult(pickIntent, 0);

i have also added the an onActivityResult() method without writing any code in it. What code do i need in the onActivityResult() method to get the package name of the app the user chose?

(or any other information with which i can launch the chosen app)


回答1:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
        ComponentName componentName = data.getComponent();
        final String packageName = componentName.getPackageName();
        final String activityName = componentName.getClassName();
    }
}

or you can just startActivity(data); so you can launch which app that user choosed.



来源:https://stackoverflow.com/questions/20295802/get-name-of-the-app-the-user-chose-from-app-list-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!