將app變爲桌面程序,開機後不再顯示原桌面,而是顯示我們的app界面

步驟一:在項目清單跟節點加入android:installLocation="internalOnly",指定你的app安裝到內存中。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxxxxx.xxx"
    android:installLocation="internalOnly">

步驟二:在第一個要啓動的activity的filter中加入<intent-filter>,讓app成爲主程序。

    <activity
        android:name=".ui.SplashAty"
        android:theme="@style/AppSplash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" /> 
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

!!!!!!!!!完工!!!!!!!!!!!

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

 

發佈了102 篇原創文章 · 獲贊 94 · 訪問量 52萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章