写点什么

Android 实现双 Launcher 的无缝切换,flutter 免费视频教程

发布于: 2 小时前

String examplePackageName = "com.jeejen.family"; //请修改为需要设置的 package name


String exampleActivityName = "com.jeejen.home.launcher.Launcher"; //请修改为需要设置的 launcher activity name


ComponentName defaultLauncher = null;


Intent intent = new Intent(Intent.ACTION_MAIN);


intent.addCategory(Intent.CATEGORY_HOME);


List<ResolveInfo> resolveInfoList =


pm.queryIntentActivities(intent, 0);


if (resolveInfoList != null) {


int size = resolveInfoList.size();


for (int j = 0; j < size; ) {


final ResolveInfo r = resolveInfoList.get(j);


if (!r.activityInfo.packageName.equals(examplePackageName)) {


resolveInfoList.remove(j);


size -= 1;


} else {


j++;


}


}


ComponentName[] set = new ComponentName[size];


defaultLauncher = new ComponentName(examplePackageName, exampleActivityName);


int defaultMatch = 0;


for (int i = 0; i < size; i++) {


final ResolveInfo resolveInfo =


resolveInfoList.get(i);


set[i] = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);


if (defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)) {


defaultMatch = resolveInfo.match;


}


}


IntentFilter filter = new IntentFilter();


filter.addAction(Intent.ACTION_MAIN);


filter.addCategory(Intent.CATEGORY_HOME);


filter.addCategory(Intent.CATEGORY_DEFAULT);


pm.clearPackagePreferredActivities(defaultLauncher.getPackageName());


pm.addPreferredActivity(filter, defaultMatch, set, defaultLauncher);


}


}


切换后启动当前的 Launcher,


public void startMainLauncher(){


Intent startMain = new Intent(Intent.ACTION_MAIN);


startMain.addCategory(Intent.CATEGORY_HOME);


startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


startActivity(startMain);


}


注意,如果直接切换的后 finish() 当前 Activity 会比较突兀,最后做一个延时的加载动画去



animdraw2.xml


<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>


<rotate xmlns:android="http://schemas.android.com/apk/res/android"


android:drawable="@drawable/ll_loading_outside"


android:fromDegrees="0.0"


android:pivotX="50.0%"


android:pivotY="50.0%"


android:toDegrees="360.0" /></span>


activity_main.xml


<span style="color:#000000;"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


xmlns:tools="http://schemas.android.com/tools"


android:layout_width="match_parent"


android:layout_height="match_parent"


android:orientation="vertical"


android:gravity="center"


android:layout_gravity="center"


tools:context=".MainActivity" >


<ProgressBar


android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:layout_gravity="center"


android:layout_marginTop="10dip"


android:indeterminateDrawable="@drawable/animdraw2"


android:indeterminateDuration="1500" />


</LinearLayout></span>


<span style="color:#000000;">package com.rtkj.switchlauncher;


import java.util.List;


import java.util.Timer;


import java.util.TimerTask;


import android.app.Activity;


import android.content.ComponentName;


import android.content.Intent;


import android.content.IntentFilter;


import android.content.SharedPreferences;


import android.content.SharedPreferences.Editor;


import android.content.pm.PackageManager;


import android.content.pm.ResolveInfo;


import android.os.Bundle;


import android.view.KeyEvent;


import android.view.View;


import android.view.View.OnClickListener;


import android.widget.Button;


import android.widget.Toast;


public class MainActivity extends Activity{


private Editor edit;


private SharedPreferences sp;


private Button bt_lock,bt_unlock;


private static final String DEFAULT_LAUNCHER="default_launcher";


private static final String LAUNCHER_NAME="com.snowfish.aios.launcher";


private static final String LAUNCHER_CLASS_NAME="com.snowfish.aios.launcher.MainActivity";


private static final String OLD_LAUNCHER_NAME="com.jeejen.family";


private static final String OLD_LAUNCHER_CLASS_NAME="com.jeejen.home.launcher.Launcher";


private static final String DEFAULT_OLD_MMS ="set_default_launcer_first";


private static final String DEFAULT_MMS=


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


"set_default_launcer_second";


public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;//定义屏蔽参数


Timer timer = new Timer();


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);


sp=getSharedPreferences("config_home", 0);


edit=sp.edit();


timer.schedule(task, 1000);


}


TimerTask task = new TimerTask() {


@Override


public void run() {


runOnUiThread(new Runnable() { // UI thread


@Override


public void run() {


//Toast.makeText(getApplicationContext(), "this show", 0).show();


switchHome();


finish();


}


});


}


};


public boolean onKeyDown(int keyCode, KeyEvent paramKeyEvent)


{


switch(keyCode){


case KeyEvent.KEYCODE_BACK:


case KeyEvent.KEYCODE_HOME:


case KeyEvent.KEYCODE_MENU:


//case KeyEvent.KEYCODE_VOLUME_DOWN:


//case KeyEvent.KEYCODE_VOLUME_UP:


//case KeyEvent.KEYCODE_VOLUME_MUTE:


return true;


default:


return false;


}


}


private void switchHome() {


if(sp.getBoolean(DEFAULT_LAUNCHER,true)){


edit.putBoolean(DEFAULT_LAUNCHER, false);


edit.commit();


setDefaultHome(OLD_LAUNCHER_NAME,OLD_LAUNCHER_CLASS_NAME,LAUNCHER_NAME,LAUNCHER_CLASS_NAME);


sendToBroad(DEFAULT_OLD_MMS);


killProgress(OLD_LAUNCHER_NAME,OLD_LAUNCHER_CLASS_NAME);


}else{


edit.putBoolean(DEFAULT_LAUNCHER, true);


edit.commit();


setDefaultHome(LAUNCHER_NAME,LAUNCHER_CLASS_NAME,OLD_LAUNCHER_NAME,OLD_LAUNCHER_CLASS_NAME);


sendToBroad(DEFAULT_MMS);


killProgress(LAUNCHER_NAME,LAUNCHER_CLASS_NAME);


}


}


public void sendToBroad(String msg){//发送广播通知系统替换默认的短信应用 其他文章有写


Intent intent=new Intent(msg);


sendBroadcast(intent);


}


public void killProgress(String packageName,String className){


/*Intent startMain = new Intent(Intent.ACTION_MAIN);


startMain.setComponent(new ComponentName(packageName, className));


startMain.addCategory(Intent.CATEGORY_HOME);


startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


startActivity(startMain);*/


Intent startMain = new Intent(Intent.ACTION_MAIN);


startMain.addCategory(Intent.CATEGORY_HOME);


startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


startActivity(startMain);


// overridePendingTransition(R.anim.zoomin, R.anim.zoomout);


//finish();


//android.os.Process.killProcess(android.os.Process.myPid());


}


private void setDefaultHome(String packeageName ,String className,String oldPackage,String oldName) {


PackageManager pm = getPackageManager();


IntentFilter f = new IntentFilter();


f.addAction(Intent.ACTION_MAIN);


f.addCategory(Intent.CATEGORY_HOME);


f.addCategory(Intent.CATEGORY_DEFAULT);


ComponentName component = new ComponentName(packeageName,className );


ComponentName[] components = new ComponentName[] {new ComponentName(oldPackage,oldName),component};


pm.clearPackagePreferredActivities(oldPackage);


pm.addPreferredActivity(f, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);


//pm.replacePreferredActivity(f, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);

评论

发布
暂无评论
Android 实现双Launcher的无缝切换,flutter免费视频教程