如何使用PackageManager检索拨号器/浏览器/ SMS应用程序等?如何使用、应用程序、浏览器、拨号器

2023-09-03 22:56:05 作者:梦想起航我起床

我写一个发射器,我需要找到拨号器,浏览器,短信和相机应用将作为码头上的快捷方式。

我想不同的厂商,如三星/ HTC /谷歌都将有每一种不同的包名不同的应用程序。

有没有办法使用PackageManager得到例如默认的拨号器应用程序?

编辑:

按照下面给出我最终实现像这样的建议:

 意向书我=新的意图(Intent.ACTION_CALL);
i.setData(Uri.parse(电话:123456789));
ResolveInfo resolveInfo = pm.resolveActivity(ⅰ,0);
AppModel结果= NULL;
如果(resolveInfo!= NULL){
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    如果(activityInfo!= NULL){
        如果(机器人.equals(activityInfo.packageName)){
            //没有默认的活动..第一选择
            名单< ResolveInfo> resolveInfos = m.queryIntentActivities(ⅰ,0);
            对于(ResolveInfo rInfo:resolveInfos){
                结果=新AppModel(背景下,rInfo.activityInfo.applicationInfo);
                打破;
            }

        } 其他 {
            ApplicationInfo APPINFO = activityInfo.applicationInfo;
            如果(APPINFO!= NULL){
                结果=新AppModel(背景下,APPINFO);
            }
        }

    }

}
 

解决方案

创建一个意图确定你想要做的(什么,例如,一个 ACTION_DIAL 意图用假电话号码),然后使用 PackageManager ,要么 resolveActivity() queryIntentActivities()

但是,请记住,有可能没有的是的默认为这些操作,任意数量的原因,其中包括:

该设备是不是电话

无权以任何匹配的应用程序的用户,在Android 4.3+设备提供者受限制的配置文件

用户可能为安装2+应用意图,并没有选择一个默认的( resolveActivity()将返回系统选择器)

此外,什么样的默认应用程序是会随着时间而改变,只是因为你是希望用户选择的主界面默认的应用程序将变成是你的问题的答案。

教大家如何给Internet Download Manager添加浏览器支持

I am writing a launcher and I need to find the dialer, browser, sms and camera app to put as shortcuts on the dock.

I assume different vendors like Samsung/HTC/Google will all have different apps for each of these with different package names.

Is there a way to use the PackageManager to get the default dialer app for example?

EDIT:

As per the advice given below I ended up implementing it like this:

Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:123456789"));
ResolveInfo resolveInfo = pm.resolveActivity(i, 0);
AppModel result = null;
if (resolveInfo != null) {
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    if (activityInfo != null) {
        if ("android".equals(activityInfo.packageName)) {
            // no default activity.. choose first
            List<ResolveInfo> resolveInfos = m.queryIntentActivities(i, 0);
            for (ResolveInfo rInfo : resolveInfos) {
                result = new AppModel(context, rInfo.activityInfo.applicationInfo);
                break;
            }

        } else {
            ApplicationInfo appInfo = activityInfo.applicationInfo;
            if (appInfo != null) {
                result = new AppModel(context, appInfo);
            }
        }

    }

}

解决方案

Create an Intent identifying what you want to do (e.g., an ACTION_DIAL Intent with a fake phone number), then use PackageManager and either resolveActivity() or queryIntentActivities().

However, bear in mind that there may not be a default for those operations, for any number of reasons, including:

The device is not a phone

The user does not have rights to any matching app, courtesy of restricted profiles on Android 4.3+ devices

The user may have installed 2+ apps for that Intent, and has not yet chosen a default (resolveActivity() will return the system chooser)

Also, the answer of what the default app is will change over time, just as you are hoping that the user's chosen home screen default app will change to be yours.