如何在android手机上实现app开机自动运行启动(可以打开应用界面)。
2016-08-20 20:30
1566 查看
测试项目的时候实在一个android系统的车载终端上进行的,因为没有终端没屏幕操作起来比较麻烦,所以干脆把用到的app写成开机自动运行,就可以了。感觉不是很困难,我就答应了大师兄的这个要求,然后就在网上查询相关的代码。最终找到了这个可以完美运行,不知道是不是有bug或者有更好的方法,希望大家指点。
首先说一下我所知道的这个代码是如何实现的。android系统在开机的过程中(可能是最后)会发送一个广播android.intent.action.BOOT_COMPLETED,这个广播的意思就是设备告诉我们“”我已经开机了!”然后我们可以在代码中用一个广播接受者来接受这个广播,然后触发开启app应用的操作。
首先在你的工程上建一个广播接受的类来:
package edu.hrbeu.xiaox.autosetup; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * @author 编写人: xiaox * @date 创建时间: 2016/8/20 * @Description 功能描述: 该类用来实现该app开机自动运行 */ public class BootBroadcastReceiver extends BroadcastReceiver { /** * demo2: 可以实现开机自动打开软件并运行。 */ @Override public void onReceive(Context context, Intent intent) { Log.d("XRGPS", "BootReceiver.onReceive: " + intent.getAction()); System.out.println("自启动程序即将执行"); //MainActivity就是开机显示的界面 Intent mBootIntent = new Intent(context, MainActivity.class); //下面这句话必须加上才能开机自动运行app的界面 mBootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mBootIntent); } }
然后我们要在AndroidManifest.xml中加入权限和配置相关信息,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在application标签中,配置相关信息:
<pre name="code" class="java"><!--开机广播接受者--> <receiver android:name=".BootBroadcastReceiver"> <intent-filter> <!--注册开机广播地址--> <action android:name="android.intent.action.BOOT_COMPLETED"></action> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
好了,这样就可以开机自动启动app了。
相关文章推荐
- android如何实现开机自动启动Service或app
- 【转】android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app
- Android开发实践经验谈:如何让各式手机浏览器可以直接下载apk安装文件且实现自动安装APP
- android如何实现开机自动启动Service或app
- android如何实现开机自动启动Service或app