【问题标题】:How can i search the application name not the package name and open it?如何搜索应用程序名称而不是包名称并打开它?
【发布时间】:2016-07-22 11:00:51
【问题描述】:

这是我在主要活动中的代码..

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button bClock = (Button) findViewById(R.id.buttonSearch);

    bClock.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String packageName = "com.android.gallery3d";
            Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);

            if(intent == null) {
                Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                marketIntent.setData(Uri.parse("market://details?id=packagename"));
                startActivity(marketIntent);
            }
            startActivity(intent);
            }
        });
    }

我的问题是是否可以不使用包名(com.android.gallery3d)来搜索我的所有应用程序?..例如,我想搜索“facebook”,然后输入它然后搜索。

因为如果我使用该软件包名称,我将不会打开新安装的应用程序。在此先感谢。

【问题讨论】:

    标签: android eclipse android-studio


    【解决方案1】:

    你可以使用 PackageManager

    final PackageManager pm = context.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo(packageName, 0);
    } catch (final NameNotFoundException e) {
        ai = null;
    }
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    

    还有这个

    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
    

    这将返回在其清单的<application> 标记中定义的应用程序名称。

    【讨论】:

    • @jvpintang 活动中的任何地方
    • 我把你的第一个代码放在我的按钮 onClick 中,我把第二个代码放在哪里?
    • 因为我有错误“无法解析符号‘上下文’”和“无法解析符号‘信息’”
    • 在顶部添加一个变量 Context context=this;
    • 好的。谢谢。第二个代码中的“无法解析符号'信息'”怎么样?
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多