通过广播来实现开机启动activity
1.创建activity
2.创建广播
3.在清单文件中注册
具体实现:
1、界面Activity:SayHello.java
package com.ghstudio.BootStartDemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SayHello extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello. I started!");
setContentView(tv);
}
}
2、接收广播消息:BootBroadcastReceiver.java
package com.ghstudio.BootStartDemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)){
Intent sayHelloIntent=new Intent(context,SayHello.class);
sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(sayHelloIntent);
}
}
}
3、配置文件:AndroidManifest.xml
package="com.ghstudio.BootStartDemo"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">
android:name="android.permission.RECEIVE_BOOT_COMPLETED">
4、完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机。