安卓开机 重启 自启动程序 亲测可用

来源:互联网 发布:印度山地师 知乎 编辑:程序博客网 时间:2024/06/06 07:11

01 添加权限(AndroidManifest.xml文件)

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

02 注册receiver(AndroidManifest.xml文件)

<receiver android:name="com.example.startup.BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

03 写receiver

public class BootBroadcastReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
try {
Thread.sleep(1000);//可要可不要
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(context, "接收到信息", 0).show();
Intent ootStartIntent = new Intent(context, MainActivity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
}

注意事项:

01:模拟器上测试不会成功,不要在模拟器上测试

02:手机端测试安装的时候要勾选程序可自启动,否则测试不会成功

源码地址http://download.csdn.net/detail/yaqiang520/8737317

0 0
原创粉丝点击