要实现一个开发板只运行一个app,开机运行,并且好像整个app就像是系统一样的效果:
第一种实现方法:通过监听开机广播 实现开机启动APP(这种方法经测试后,首先会进入系统launcher页面后大概10秒后启动自己的app,不是我要的效果)
1.1 写一个广播接收器,用来接收手机开机广播
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Intent service = new Intent(context,XXXclass);
// context.startService(service);
//启动应用,参数为需要自动启动的应用的包名
Intent i = context.getPackageManager().getLaunchIntentForPackage("com.yichen.yccamera");
context.startActivity(i);
/* //另一种启动方法,网上查来的,因为和我要的效果有差距,我没试这种方法
Intent mBootIntent = new Intent(context, MainActivity.class);
// 必须设置FLAG_ACTIVITY_NEW_TASK
mBootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mBootIntent);*/
}
}
1.2 manifest中静态注册广播接收器
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
1.3 加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
第一种方法效果不理想,改搜索关键词为“安卓开机显示app launcher页面”,实现开机直接运行app,并把app主页面当成手机桌面
2 manifest中将Launcher Activity 加HOME和DEFAULT(不用操作第一种思路,直接加了就有效果)
<activity
android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>-->这句可有可无
</intent-filter>
</activity>
一般这样操作之后安装app重启,系统会让选择是使用系统的 launcher 还是自己的 launcher,有“始终”和“仅此一次”两种选项。
(很多人到这里就以为可以了,结果重启手机发现还是不行,看下面)
到这里,离成功只差关键一步,就是你需要在手机设置中找到 桌面设置
选项,找不到的可以在设置中的搜索栏搜一下,找到后你会发现桌面设置这里多了一个选项,就是你的这个app,因为加了
<category android:name="android.intent.category.HOME" />这一行,桌面设置也会把你的app当成一个桌面主题,还有一个选项是系统桌面,你需要设置成自己的app,重启手机,ok!
(我测试了华为5.1华为7.0和小米5.2三部手机,均能找到桌面设置这个选项)
如果找不到桌面设置选项,可以从手机设置–应用程序-查看所有应用程序(包括系统的应用程序),找到桌面程序之类的字眼的应用,清楚其默认设置。清除之后回到APP,按回主页面的时候应该会有类似选择默认桌面的设置项。
参考链接:
https://blog.youkuaiyun.com/u014628886/article/details/67642621 APP从启动到主页面显示,经历了哪些过程(很理论的东西,可以经常看看)
https://blog.youkuaiyun.com/louiswangbing/article/details/6607612