美文网首页安卓开发手机移动程序开发Android 开发技术分享
PackageManager应用名(程序名)获取应用包名

PackageManager应用名(程序名)获取应用包名

作者: 九狼JIULANG | 来源:发表于2020-09-22 11:06 被阅读0次
    效果图

    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><?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"

    相关文章

      网友评论

        本文标题:PackageManager应用名(程序名)获取应用包名

        本文链接:https://www.haomeiwen.com/subject/ngxwyktx.html