您的位置:首页 > 移动开发 > Android开发

【Android】程序自启动,跳过主桌面(Launcher)

2014-12-17 15:56 573 查看
最近项目需要做一个安卓端的控制程序,其中一条需求就是开机自动启动,按照网上很多说说法是通过BroadcastReceiver的方式去实现的,之前使用这样的方法还算可以,开进进入主桌面,很快就能跳转到自己的应用程序中,但是这次买的新板子,不知道是硬件配置问题还是各个厂商提供的系统差异性,新板子以这种方式实现开机启动,会在主桌面(Launcher)停留很长时间,让人无法接受,因此第一想法就是是否能通过删除Launcher的方式来解决这个问题(Launcher通常存放在/system/app/Launcher2.apk,如何删除参考http://blog.csdn.net/hua583999775/article/details/41981315),删除后发现系统一直停留在开机界面(就是android闪耀的界面),根本不会进入自己的程序,然后就想自己的程序是否能替代Launcher的功能呢,网上找了些资料,下面做一下总结。

首先讲一下BroadcastReceiver的方式实现开机自起的功能:

BroadcastReceiver是广播机制,系统完全启动后会发出一个BOOT_COMPLETED的广播,我们创建一个类,继承BroadcastReceiver,实时监听BOOT_COMPLETED的广播消息,监听到后启动我们自己的应用程序即可,代码如下

public class AutoBootBroadcase extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent intent1 = new Intent();
intent1.setClass(context, MainActivity.class);
intent1.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}

}在AndroidManifest.xml文件中申明这个类
<receiver
android:name=".AutoBootBroadcase">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

另一种方法就是模仿Launcher,让我们的程序实现Launcher的功能,据了解系统启动后,由ActivityManager调用Launcher(网上查阅的资料,还有待证实),AcitvityManager会启动拥有CATEGORY_HOME属性的Activity,Laucher的<intent-filter>是这么描述的
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>一定程度上也验证了上面的说法,我们只需要在我们的应用的主Activity的<intent-filter>中添加<category android:name="android.intent.category.Home"/>就可以实现类似Launcher的功能了,具体还是需要了解安卓的启动原理,Launcher的启动机制才能很好的去解决这个问题,这边由于项目时限问题,做个记录,等有时间再好好研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android