Android默认桌面设置终级大招

最近研究桌面程序开发,遇到一个难题,就是按Home键没法选择自己的桌面作为默认桌面,经过一番辛苦的查找组合,终于从各个旮旯挖出代码,然后组合修改,调试,终于修成正果。各位看官,直接看代码:


import android.app.Activity;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Settings;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.SuperscriptSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.DigitalClock;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.disney.sclocker.R;
import com.disney.sclocker.alarm.ClockAlarmActivity;
import com.disney.sclocker.locker.ScreenLockService;
import com.disney.sclocker.util.Utils;
import com.disney.sclocker.widget.DisneyAnalogClockV1;
import com.disney.sclocker.widget.GuideViewPager;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class DesktopActivity extends Activity {
    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        


        setContentView(R.layout.activity_desktop);
        
        //HOME键侦测
        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(homeReceiver, homeFilter);
        Log.d("123", "android.os.Build.MANUFACTURER=" + android.os.Build.MANUFACTURER);
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                return true;
            case KeyEvent.KEYCODE_HOME:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }




    //Home键侦测---[----
    private final BroadcastReceiver homeReceiver = new BroadcastReceiver() {
        final String SYS_KEY = "reason"; // 标注下这里必须是这么一个字符串值


        final String SYS_HOME_KEY = "homekey";// 标注下这里必须是这么一个字符串值


        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYS_KEY);
                if (reason != null && reason.equals(SYS_HOME_KEY)) {
                    Log.i("TT", "home键监听");
                    String currentHome = getHomeLauncher();
                    Log.i("TT", "currentHome="+currentHome);
                    if (isDefaultHome()) {
                        return;
                    }
                    setDefaultL();




                }
            }
        }
    };


    private void setDefaultL(){


        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory("android.intent.category.HOME");
        try {
            intent.setComponent(new ComponentName("android","com.android.internal.app.ResolverActivity"));
            startActivity(intent);
        }catch (Exception e){//这里就是为了处置华为手机的
            try {

intent.setComponent(new ComponentName("com.huawei.android.internal.app", "com.huawei.android.internal.app.HwResolverActivity"));//这个类有些华为手机找不到
                startActivity(intent);
            } catch (Exception e1){
                e1.printStackTrace();
                try {
                    startHuaweiSettingActOfDefLauncher();//开启桌面设置
                }catch(Exception e2){
                    e2.printStackTrace();
                    intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);//还不行,就只能应用程序设置了
                    startActivity(intent);
                }


            }
        }
    }


    /**
     * 判断自己是否为默认桌面
     */
    public final boolean isDefaultHome() {
        Intent intent = new Intent(Intent.ACTION_MAIN);//Intent.ACTION_VIEW
        intent.addCategory("android.intent.category.HOME");
        intent.addCategory("android.intent.category.DEFAULT");
        PackageManager pm = getPackageManager();
        ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        boolean isDefault = getPackageName().equals(info.activityInfo.packageName);
        return isDefault;
    }


    public void startHuaweiSettingActOfDefLauncher() {
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction(Intent.ACTION_MAIN);//"android.intent.action.MAIN"
        localIntentFilter.addCategory(Intent.CATEGORY_HOME);//"android.intent.category.HOME"
        Intent localIntent3 = new Intent(localIntentFilter.getAction(0));
        localIntent3.addCategory(localIntentFilter.getCategory(0));
        Intent localIntent4 = new Intent();
        localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent4.setClassName("com.android.settings", "com.android.settings.Settings$PreferredSettingsActivity");
        localIntent4.putExtra("preferred_app_package_name", getPackageName());
        localIntent4.putExtra("preferred_app_class_name", DesktopActivity.class.getName());
        localIntent4.putExtra("is_user_confirmed", true);
        localIntent4.putExtra("preferred_app_intent", localIntent3);
        localIntent4.putExtra("preferred_app_intent_filter", localIntentFilter);
        localIntent4.putExtra("preferred_app_label", "默认桌面设置");
        startActivity(localIntent4);
    }


    


    private String getHomeLauncher() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, 0);
        String currentHomePackage = resolveInfo.activityInfo.packageName;
        return currentHomePackage;
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(homeReceiver);
        super.onDestroy();
    }
  
}

你可能感兴趣的:(Android默认桌面设置终级大招)