how to make the Launcher-Pick-Up popup window showing?

被刻印的时光 ゝ 提交于 2019-12-09 22:06:23

问题


I have an Launcher app installed. And user has choose another launcher as default, that means when pressing HOME the default launcher will come to front.

I wanna supply user with an convenience of reseting default launcher. Such as a button clicking in my launcher's UI will make Launcher-Pick-Up popup window showing.

Go Launcher can do that(in Go's setting view). it seems Go Launcher does something like "PackageManager.clearPackagePreferredActivities("com.android.launcher")" to clear the prefered launcher activity!

how to achive that ?


回答1:


launcher can only clear its own prefered settings for security issue.

here is a work around:

register a mockup activity in Manifest.xml:

   <activity
        android:name="MockupLauncher"
        android:enabled="false" 
        android:exported="false" 
        android:excludeFromRecents="true" 
        android:launchMode="singleTask" >

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

use "PackageManager.setComponentEnabledSetting" to trigger OS clear current prefered launcher:

    private void resetPreferedLauncher() {

       PackageManager pm = mContext.getPackageManager();
       ComponentName mockupComponent = new  ComponentName(MockupLauncher.class.getPackage().getName(), MockupLauncher.class.getName());

       pm.setComponentEnabledSetting(mockupComponent,  PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

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

      pm.setComponentEnabledSetting(mockupComponent,  PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
      // or
      //pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
   }

that is how I make it! enjoy!



来源:https://stackoverflow.com/questions/20471350/how-to-make-the-launcher-pick-up-popup-window-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!