PackageManager應用名(程式名)獲取應用包名

2020-09-23 12:00:47

先上圖
效果圖
JAVA

/**
 * Create by JIULANG on 2020
 */
public class MainActivity extends Activity 
{
	ListView listView;
	List<String> listdata= new ArrayList<>();
	Button btn ;
	EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		btn = findViewById(R.id.mainButton1);
		et = findViewById(R.id.mainEditText1);
		btn.setText("查詢");
		btn.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String edit = et.getText().toString();

					et.setText("");				
					et.setHint(edit+" :"+getpackageName(edit));

				}		
			});
		getAppInfo() ;


		
		et.setHint("應用計數: "+listdata.size() + "個");
		final String strdata[] = new String[listdata.size() - 1];//假資料
        for (int i=0;i < listdata.size() - 1;i++)
		{
			strdata[i] = listdata.get(i) ;
        }
		//放個ListView視覺化
		listView = findViewById(R.id.List1);
		//List介面卡
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, strdata);//新建並設定ArrayAapeter
        listView.setAdapter(adapter);

    }

	private String getpackageName(String Name)
	{
		LinkedHashMap<String,String> map =getAppInfo();
		if (map != null)
		{
			//位置計數
            int index = 0;
            boolean hasContains = false;

			//Map遍歷
            Object[] keys = map.keySet().toArray();
            for (int i = 0; i < keys.length; i++)
			{

                String key = (String) keys[i];
                if (key.equals(Name))
				{
                    index = i;
                    hasContains = true;
                    break;
                }
            }
            if (hasContains){
			
				//返回包名
				return (String)map.values().toArray()[index];
			}
		}

		return "該應用不存在!";
	}





	private LinkedHashMap<String,String> getAppInfo()
	{
		//Map
		LinkedHashMap<String,String> packagesmap = new LinkedHashMap<>();
		
		//包管理器
		PackageManager  pm = this.getPackageManager();
        List<PackageInfo> piList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES |
														   PackageManager.GET_CONFIGURATIONS |
														   PackageManager.GET_GIDS |
														   PackageManager.GET_INSTRUMENTATION |
														   PackageManager.GET_INTENT_FILTERS |
														   PackageManager.GET_META_DATA |
														   PackageManager.GET_PERMISSIONS |
														   PackageManager.GET_PROVIDERS |
														   PackageManager.GET_RECEIVERS |
														   PackageManager.GET_SERVICES |
														   PackageManager.GET_SHARED_LIBRARY_FILES |
														   PackageManager.GET_SIGNATURES |
														   PackageManager.GET_URI_PERMISSION_PATTERNS);

        if (piList != null)
		{
            for (PackageInfo pi : piList)
			{		
				listdata.add("程式名: " + pi.applicationInfo.loadLabel(pm).toString()+"\n" + "包名:" + pi.packageName +"\n"+ "包共用user id" + pi.sharedUserId);
				//Map插入
                packagesmap.put(pi.applicationInfo.loadLabel(pm).toString(),/*包名*/pi.packageName);
			}
		}
		return packagesmap;
	}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<EditText
		android:layout_width="wrap_content"
		android:ems="10"
		android:layout_height="wrap_content"
		android:id="@+id/mainEditText1"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:id="@+id/mainButton1"/>

	<ListView
		android:layout_width="wrap_content"
		android:layout_height="match_parent"
		android:id="@+id/List1"/>

</LinearLayout>

點個贊吧!