机器人:选择默认的启动程序编程机器人、程序

2023-09-13 01:22:19 作者:社会从不相信眼泪

我要弹出一个对话框,让用户选择一个发射器将推出的与设置为默认选项。我试过

I want to pop up a dialog that lets the user choose a launcher to be launched with set as default option. I tried

        Intent home = new Intent(Intent.ACTION_DEFAULT);
    home.addCategory(Intent.CATEGORY_LAUNCHER);
    Intent chooser = Intent.createChooser(home, "Launcher");
    context.startActivity(chooser);

但是,这个弹出的对话框没有设置默认的选项。虽然下面的code不会弹出的对话框中,如果默认的启动已设置。

But the dialog popped by this does not have the option to set default. While the following code will not pop up the dialog if a default launcher is already set.

        Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);

如何才能做到这一点?感谢您的帮助提前。

How can this be done? Thanks for help in advance.

推荐答案

尝试使用以下内容:

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

如果默认动作已经设置(您),你可以先拨打:

If a default action is already set (yours), you can call first:

getPackageManager().clearPackagePreferredActivities(getPackageName());

如果默认操作是不是你的,你不能以编程方式清除它,你可以做的是检查是否有其他的应用程序设置为默认值,并显示一条消息。

If the default action is not yours, you cannot clear it programmatically, what you can do is to check if other app is set as default and show a message..

private boolean isMyLauncherDefault() {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);

    final String myPackageName = getPackageName();
    List<ComponentName> activities = new ArrayList<ComponentName>();
    PackageManager packageManager = (PackageManager) getPackageManager();

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);

    if(activities.size() == 0) //no default
        return true;

    for (ComponentName activity : activities) {
        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}

由于在本案的其他应用程序的一种解决方法是设置为默认值,你可以创建一个家,安装(这将ForSE会议系统来清除默认应用程序)然后将其卸载......

As a workaround in case of other app is set as default, you can created a fake home, install it (this will forse the system to clear the default app) and then uninstall it...

的Manifest.xml

<activity
        android:name="FakeHome"  android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

FakeHome.java

public class FakeHome extends Activity {

}

某处

if(!isMyLauncherDefault()) {            
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(Activity.this, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);            
startActivity(selector);

p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);            
    }
 
精彩推荐