Android 如何通过编程设置默认启动器?
我想将我的启动器设置为默认启动器。我的代码对很多人都能正常工作,但在LeTtv设备上不能正常工作。 因为它是从默认应用程序设置中设置默认启动器的 当运行此代码时,它会在默认启动器上移动,但不会仅在LeTv设备中显示启动器选择器弹出窗口 如何打开默认应用程序选择设置? 选择默认启动器的我的代码Android 如何通过编程设置默认启动器?,android,default,android-launcher,Android,Default,Android Launcher,我想将我的启动器设置为默认启动器。我的代码对很多人都能正常工作,但在LeTtv设备上不能正常工作。 因为它是从默认应用程序设置中设置默认启动器的 当运行此代码时,它会在默认启动器上移动,但不会仅在LeTv设备中显示启动器选择器弹出窗口 如何打开默认应用程序选择设置? 选择默认启动器的我的代码 private void setDefLauncher(Context c) { PackageManager p = c.getPackageManager(); Component
private void setDefLauncher(Context c) {
PackageManager p = c.getPackageManager();
ComponentName cN = new ComponentName(c, FakeLauncher.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);
c.startActivity(selector);
p.setComponentEnabledSetting(cN,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
AndroidManifest.xml
<activity
android:name="com.android.launcher.launcher3.Launcher"
android:clearTaskOnLaunch="true"
android:enabled="true"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:resumeWhilePausing="true"
android:screenOrientation="nosensor"
android:stateNotNeeded="true"
android:taskAffinity=""
android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
<activity
android:name="com.launcher2.activitys.FakeLauncher"
android:enabled="false"
android:excludeFromRecents="true" >
<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>
我在谷歌搜索,StackOverFlow和Github但我什么也没找到,
幸运的是,一些午餐者帮我在Levt&MI手机上重新安排了寄宿活动,它的工作符合我的期望。
在某些情况下,它的工作是完美的,我只是将其反编译并使用代码
对于设置默认启动器
new SetDefaultLauncher(activity).launchHomeOrClearDefaultsDialog();
package com.android.launcher;
导入java.util.List;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.net.Uri;
导入android.os.Build.VERSION;
导入android.text.SpannableString;
导入android.text.style.TtsSpan.TextBuilder;
导入com.example.launcher.R;
导入com.launcher2.activitys.FakeLauncher;
公共类SetDefaultLauncher{
publicstaticfinalstringlauncher\u CLASS=“com.android.LAUNCHER.launcher3.LAUNCHER”;
公共静态最终字符串启动器\u PACKAGE=“com.android.LAUNCHER”;
活动;
SetDefaultLauncher(活动){
这个。活动=活动;
}
枚举家园{
GEL_为_默认值,其他_启动器_为_默认值,无_默认值
}
公共布尔启动homeorclearnaultsdialog(){
Intent-Intent=newintent(“android.Intent.action.MAIN”);
intent.addCategory(“android.intent.category.HOME”);
ResolveInfo resolveActivity=activity.getPackageManager().resolveActivity(
意图,0);
HomeState HomeState=(启动程序包)
.equals(resolveActivity.activityInfo.applicationInfo.packageName)和&LAUNCHER_类
.equals(resolveActivity.activityInfo.name))?HomeState.GEL是默认值
:(resolveActivity==null
||resolveActivity.activityInfo==null | |!在ResolveInfoList中(
resolveActivity,activity.getPackageManager()
.QueryInputActivities(intent,0))?HomeState.NO\u默认值
:HomeState.OTHER_LAUNCHER_为默认值;
交换机(主状态){
案例GEL_为默认值:
案例编号\u默认值:
intent=newintent(“android.intent.action.MAIN”);
intent.addCategory(“android.intent.category.HOME”);
意图.设置标志(268435456);
活动。开始触觉(意图);
返回true;
违约:
showClearDefaultsDialog(解析活动);
返回false;
}
}
@SuppressLint(“NewApi”)私有无效showClearDefaultsDialog(ResolveInfo ResolveInfo){
字符序列字符串;
最终意图;
CharSequence loadLabel=resolveInfo.loadLabel(activity.getPackageManager());
如果(VERSION.SDK_INT<21
||activity.getPackageManager().resolveActivity(
新意图(“android.settings.HOME\u settings”),0)=null){
string=activity.getString(R.string.change\u default\u home\u dialog\u body,
新对象[]{loadLabel});
意图=新意图(
“android.settings.APPLICATION\u详细信息\u设置”,
fromParts(“包”,
resolveInfo.activityInfo.packageName,null);
}否则{
intent=新intent(“android.settings.HOME_settings”);
string=new SpannableString(activity.getString(
R.string.change\u default\u home\u对话框\u body\u设置,
新对象[]{loadLabel});
((SpannableString)字符串)
.固定盘(
新文本生成器(
activity.getString(
R.string.change\u default\u home\u dialog\u body\u settings\u tts,
loadLabel().build(),0,字符串
.长度(),18);
}
新建AlertDialog.Builder(活动)
.setIcon(R.drawable.ic_启动器)
.setMessage(字符串)
.setNegativeButton(
activity.getString(R.string.change\u default\u home\u dialog\u cancel),
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
activity.finish();
}
})
.setOnCancelListener(新的DialogInterface.OnCancelListener(){
@凌驾
public void onCancel(对话框接口对话框){
//TODO自动生成的方法存根
activity.finish();
}
})
.setPositiveButton(
activity.getString(R.string.change\u default\u home\u dialog\u procedue),
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
package com.android.launcher;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build.VERSION;
import android.text.SpannableString;
import android.text.style.TtsSpan.TextBuilder;
import com.example.launcher.R;
import com.launcher2.activitys.FakeLauncher;
public class SetDefaultLauncher {
public static final String LAUNCHER_CLASS = "com.android.launcher.launcher3.Launcher";
public static final String LAUNCHER_PACKAGE = "com.android.launcher";
Activity activity;
SetDefaultLauncher(Activity activity){
this.activity=activity;
}
enum HomeState {
GEL_IS_DEFAULT, OTHER_LAUNCHER_IS_DEFAULT, NO_DEFAULT
}
public boolean launchHomeOrClearDefaultsDialog() {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
ResolveInfo resolveActivity = activity.getPackageManager().resolveActivity(
intent, 0);
HomeState homeState = (LAUNCHER_PACKAGE
.equals(resolveActivity.activityInfo.applicationInfo.packageName) && LAUNCHER_CLASS
.equals(resolveActivity.activityInfo.name)) ? HomeState.GEL_IS_DEFAULT
: (resolveActivity == null
|| resolveActivity.activityInfo == null || !inResolveInfoList(
resolveActivity, activity.getPackageManager()
.queryIntentActivities(intent, 0))) ? HomeState.NO_DEFAULT
: HomeState.OTHER_LAUNCHER_IS_DEFAULT;
switch (homeState) {
case GEL_IS_DEFAULT:
case NO_DEFAULT:
intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
intent.setFlags(268435456);
activity.startActivity(intent);
return true;
default:
showClearDefaultsDialog(resolveActivity);
return false;
}
}
@SuppressLint("NewApi") private void showClearDefaultsDialog(ResolveInfo resolveInfo) {
CharSequence string;
final Intent intent;
CharSequence loadLabel = resolveInfo.loadLabel(activity.getPackageManager());
if (VERSION.SDK_INT < 21
|| activity.getPackageManager().resolveActivity(
new Intent("android.settings.HOME_SETTINGS"), 0) == null) {
string = activity.getString(R.string.change_default_home_dialog_body,
new Object[] { loadLabel });
intent = new Intent(
"android.settings.APPLICATION_DETAILS_SETTINGS",
Uri.fromParts("package",
resolveInfo.activityInfo.packageName, null));
} else {
intent = new Intent("android.settings.HOME_SETTINGS");
string = new SpannableString(activity.getString(
R.string.change_default_home_dialog_body_settings,
new Object[] { loadLabel }));
((SpannableString) string)
.setSpan(
new TextBuilder(
activity.getString(
R.string.change_default_home_dialog_body_settings_tts,
loadLabel)).build(), 0, string
.length(), 18);
}
new AlertDialog.Builder(activity)
.setIcon(R.drawable.ic_launcher)
.setMessage(string)
.setNegativeButton(
activity.getString(R.string.change_default_home_dialog_cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
activity.finish();
}
})
.setOnCancelListener(new DialogInterface. OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
activity.finish();
}
})
.setPositiveButton(
activity.getString(R.string.change_default_home_dialog_proceed),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
try {
intent.setFlags(276856832);
activity.startActivity(intent);
} catch (Exception e) {
setDefLauncher(activity);
}
}
}).create().show();
}
private boolean inResolveInfoList(ResolveInfo resolveInfo,
List<ResolveInfo> list) {
for (ResolveInfo resolveInfo2 : list) {
if (resolveInfo2.activityInfo.name
.equals(resolveInfo.activityInfo.name)
&& resolveInfo2.activityInfo.packageName
.equals(resolveInfo.activityInfo.packageName)) {
return true;
}
}
return false;
}
private void setDefLauncher(Context c) {
PackageManager p = c.getPackageManager();
ComponentName cN = new ComponentName(c, FakeLauncher.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);
c.startActivity(selector);
p.setComponentEnabledSetting(cN,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}