项目文件目录:
只需如下两个步骤即可:
步骤一: 添加广播:
package com.broadcast;
import com.example.signaturedemo1.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//MainActivity就是开机显示的界面
Intent mBootIntent = new Intent(context, MainActivity.class);
//下面这句话必须加上才能开机自动运行app的界面
mBootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mBootIntent);
}
}
步骤二: AndroidManifest.xml中加入权限和配置相关信息
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--开机广播接受者-->
<receiver android:name="com.broadcast.BootBroadcastReceiver">
<intent-filter>
<!--注册开机广播地址-->
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
既可以打开APP主界面了,附上MainActivity主代码:
package com.example.signaturedemo1;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);<span style="font-family: 'Microsoft YaHei'; white-space: pre-wrap;">} </span><span style="font-family: 'Microsoft YaHei'; white-space: pre-wrap;">}</span>