有的时候,我们需要让安卓机器开机后自动跳转运行我们自己的应用程序,而不是进入安卓HOME界面。有2种办法可以实现该目的。
一、开机后自动进入程序:
当 Android 启 动 时 , 会 发 出 一 个 系 统 广 播 , 内 容 为ACTION_BOOT_COMPLETED , 它 的 字 符 串 常 量 表 示 为android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。所以实现的手段就是实现一个 BroadcastReceiver。
1.首先定义1个activity,窗口设置为无标题和全屏
BootStartDemo.java
<span style="font-size:18px;">public class BootStartDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 无 title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}</span>
BootBroadcastReceiver.java
<span style="font-size:18px;">public class BootBroadcastReceiver extends BroadcastReceiver {
static final String action_boot="android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(action_boot)){
Intent ootStartIntent=new Intent(context,BootStartDemo.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
}
}</span>
3.配置文件 ,其中红色地方需要修改
AndroidManifest.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<!-- 这是一个开机自启动程序 -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ajie.bootstartdemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BootStartDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<span style="color:#ff0000;"><receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver></span>
</application>
<span style="color:#ff0000;"><uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission></span>
</manifest></span>