1. 程式人生 > >Android預設桌面設定終級大招

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();
    }
  
}