Android实现应用的开机启动
效果是在车载屏幕实现的,并不是手机,我这块没找到6.0以下手机,所以用的模拟机测试(在下面),6.0以上的手机貌似不管用,我在试试...
献上效果图
zidong2.gif
本文摘自:
https://blog.csdn.net/qq_24712507/article/details/108572758
https://blog.csdn.net/jez/article/details/85762212
https://blog.csdn.net/qq_24712507/article/details/108572758
实现逻辑:
拦截广播android.permission.RECEIVE_BOOT_COMPLETED,并且需要使用静态注册广播的方法
上代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.statebarandnavigationbarhide_demo"
>
<!-- android:installLocation="internalOnly" 应用安装位置手机本身-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- <uses-permission android:name="android.permission.READ_SETTINGS" />-->
<!-- <uses-permission android:name="android.permission.WRITE_SETTINGS" />-->
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>-->
<!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- android:usesCleartextTraffic="true"-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver">
<!-- enabled 能否接收其他APP发出的广播
默认值是由receiver中是否有intent-filter来决定的;如果有true,没有false-->
<!-- <intent-filter android:priority="1000">-->
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
广播
package com.example.statebarandnavigationbarhide_demo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* @auther eerdunsang
* @date 2020/12/3
* @time 9:39.
*/
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("tag", "onReceive: "+"广播调起来了");
String ACTION = "android.intent.action.BOOT_COMPLETED";
String action = intent.getAction();
if (action.equals(ACTION)){
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.i("tag", "onReceive: "+"通过广播监听权限启动项目");
}
}
}
Activity
package com.example.statebarandnavigationbarhide_demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "启动了", Toast.LENGTH_SHORT).show();
Log.i("tag", "onCreate: "+"开机启动了");
}
}
帖子很多照样摘下来发现还是不能实现,最后添加了网络权限之后才实现了,具体没去追究。在调试时候没有实现功能的可以把注解解开一个个试试。
原文下载demo:github
我又找到一个比较简单的 补上!! 手机版!! 测试模拟器 Nexus
image.png image.png有点不清楚 设备是5.1.1的
代码比上面的简单
Nexus5.1.1 效果图
开机启动.gif
同样我 下载了Nexus 6.0 效果同上!!
image.png image.png这是pixel 8.0 效果
image.png 开机启动_Pixel_8.0.gif↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 代码部分↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
image.pngimage.pngactivity 什么也没写
广播监听
package com.phone.autostartdemo.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class StartReceiver extends BroadcastReceiver {
private String TAG = "StartReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive-------");
Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
context.startActivity(i);
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phone.autostartdemo">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.StartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
</application>
</manifest>
网友评论