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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将指定的应用程序显示在 MTK Android 系统的桌面上,你需要执行以下步骤: 1. 在 AndroidManifest.xml 文件中声明应用程序的主 Activity。确保在 intent-filter 中设置了 CATEGORY_LAUNCHER,这样应用程序的图标才会显示桌面上。 2. 在应用程序的主 Activity 中,使用以下 Intent 启动应用程序: ``` Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); launchIntent.setComponent(new ComponentName(this, MainActivity.class)); startActivity(launchIntent); ``` 在上面的代码中,MainActivity.class 要替换成你应用程序的主 Activity。 3. 如果你想在系统启动时自动启动应用程序,你可以创建一个 BroadcastReceiver,监听 ACTION_BOOT_COMPLETED 广播,并在该广播中启动应用程序。 ``` public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent launchIntent = new Intent(context, MainActivity.class); launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(launchIntent); } } } ``` 在上面的代码中,MainActivity.class 要替换成你应用程序的主 Activity。 然后在 AndroidManifest.xml 文件中声明该 BroadcastReceiver: ``` <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> ``` 确保在 AndroidManifest.xml 文件中声明了 RECEIVE_BOOT_COMPLETED 权限: ``` <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> ``` 这样,你的应用程序就会显示在 MTK Android 系统的桌面上了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值