将app变为桌面程序,开机后不再显示原桌面,而是显示我们的app界面
16lz
2021-01-25
步骤一:在项目清单跟节点加入android:installLocation="internalOnly",指定你的app安装到内存中。
步骤二:在第一个要启动的activity的filter中加入
!!!!!!!!!完工!!!!!!!!!!!
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 {@Overrideprotected 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);}@Overridepublic 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;}@Overrideprotected void onDestroy() { unregisterReceiver(homeReceiver); super.onDestroy();}}
更多相关文章
- Android(安卓)4.0.1 模拟器内核 最新下载地址及编译步骤
- Android(安卓)后台定时任务
- 史上最详细创建 Android(安卓)AIDL 远程服务的步骤
- Android(安卓)应用桌面角标显示各厂商规则说明
- 下载并编译Chrome浏览器For Android
- Android:RxJava 结合 Retrofit 优雅实现 网络请求轮询
- Android(安卓)Studio中JNI程序的单步调试和日志打印
- Android(安卓)使用外部db数据库文件
- Android(安卓)10 手势导航源码分析