如何确定是否我的应用程序被设置为通过用户默认? [英] How to identify if my app is set as Default by the user?

查看:157
本文介绍了如何确定是否我的应用程序被设置为通过用户默认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在'X'被安装在设备和放我的主屏幕上的应用程序;当用户presses主页按钮,他与默认动作 Android的对话框中默认主页和放大器之间作出选择提示;我的X应用程序。

我的用例是保持提示用户 - 有点罕见,虽然,这种默认操作对话框,直到他选择我的应用程序为默认值。


  1. 我可以做一个 ACTION_MAIN 意图提示。

  2. 问题是,当我应该停止提示?结果
    我怎么会知道我的X应用程序是默认了吧?


解决方案

让我们假设你有一个应用X,在下面的声明其清单

 <活动机器人:名字=。MyActivity
       机器人:标签=@字符串/ APP_NAME>
    < - 过滤器:这个活动是在连续的默认视图操作
         RawContacts - >
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.VIEW/>
        <数据机器人:mime类型=vnd.android.cursor.item / raw_contact/>
        <类机器人:名字=android.intent.category.DEFAULT/>
    &所述; /意图滤光器>
< /活性GT;

要检查这个应用程序是否是默认选项(在此examepl的的东西的),你只需:

  / **
 *如果提供的组件名称是preferred活动返回true
 *任何行动。
 * @参数组成的活动组件名称,例如
 *活动#getComponentName()。
 * /
布尔ISDEFAULT(组件名组件){
    ArrayList的<&组件名GT;组件=新的ArrayList<&组件名GT;();
    ArrayList的<&的IntentFilter GT;过滤器=新的ArrayList<&的IntentFilter GT;();
    。getPackageManager()获得preferredActivities(过滤器,组件,NULL);
    返回components.contains(组件);
}

Once my Home Screen app 'X' is installed on the device & when the user presses the Home Button, he is prompted with the Default Action Android dialog to choose between the Default Home & my X app.

My use case is to keep on prompting the user - a bit infrequently though, with this Default Action dialog till he selects my App as default.

  1. I can do the prompting with a ACTION_MAIN intent.
  2. Issue is when should i stop the prompting ?
    How would i know that my X app is the default now ?

解决方案

Let's say you have an application X, with the following declaration in its manifest

<activity android:name=".MyActivity"
       android:label="@string/app_name">
    <!-- filter: This activity can be the default view action for a row in
         RawContacts -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="vnd.android.cursor.item/raw_contact" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

To check whether or not this application is the "default option" (in this examepl for anything) you simply:

/**
 * Returns true if the supplied component name is the preferred activity
 * for any action.
 * @param component The ComponentName of your Activity, e.g. 
 *    Activity#getComponentName().
 */
boolean isDefault(ComponentName component) {
    ArrayList<ComponentName> components = new ArrayList<ComponentName>();
    ArrayList<IntentFilter> filters = new ArrayList<IntentFilter>();
    getPackageManager().getPreferredActivities(filters, components, null);
    return components.contains(component);
}    

这篇关于如何确定是否我的应用程序被设置为通过用户默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆