版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37972348/article/details/81027534
Android 设置应用开机启动 崩溃重启 隐藏/显示底部虚拟按键
设置应用开机启动
两种方式可以实现
方式一:设置监听,在系统启动时,打开我们的应用
1. 新建 ContentReceiver 类
// 开机启动1/3
public class ContentReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Intent mainActivityIntent = new Intent(context, MainActivity.class); // 要启动的Activity
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
}
}
}
2. 在 app/manifests/AndroidManifest.xml 文件中添加如下代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication">
<application>
......
<!--开机自启动 2/3-->
<receiver android:name=".ContentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.LAUNCHER" />
<!--<category android:name="android.intent.category.HOME" />-->
</intent-filter>
</receiver>
</application>
<!--开机自启动 3/3-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
方式二:将应用替换默认的桌面应用。这样的好处是可以屏蔽用户操作桌面,开机后只能进入我们的应用程序,保护系统安全性。
在 app/manifests/AndroidManifest.xml 文件中添加如下代码:
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!--以下两行为替换系统自带桌面,改为启动本 app -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
设置应用崩溃重启
代码如下:
public class MyApplication extends Application {
private static MyApplication application;
@Override
public void onCreate() {
super.onCreate();
application = this;
// 程序崩溃时触发线程 以下用来捕获程序崩溃异常
Thread.setDefaultUncaughtExceptionHandler(handler);
}
private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("应用奔溃,准备重启程序...");
restartApp(); //发生崩溃异常时,重启应用
}
};
private void restartApp() {
Intent intent = new Intent(this, MainActivity.class);
@SuppressLint("WrongConstant") PendingIntent restartIntent = PendingIntent.getActivity(
application.getApplicationContext(), 0, intent,Intent.FLAG_ACTIVITY_NEW_TASK);
//退出程序
android.os.Process.killProcess(android.os.Process.myPid());
//重启程序
AlarmManager mgr = (AlarmManager)application.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000*1,
restartIntent); // 1秒钟后重启应用
}
}
隐藏/显示底部虚拟按键
隐藏底部虚拟按键:
public boolean hideNavigation(){
boolean ishide;
try
{
String command;
command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
ishide = true;
}
catch(Exception ex)
{
Toast.makeText(getApplicationContext(), ex.getMessage(),
Toast.LENGTH_LONG).show();
ishide = false;
}
return ishide;
}
显示底部虚拟按键:
public boolean showNavigation(){
boolean isshow;
String command;
command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
Process proc = null;
try {
proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
isshow = true;
return isshow;
}