[Android新手学习笔记29]-Broadcast-例二广播实习开机自启动

包名右键->New->Other->Broadcast Receiver,新建一个广播接收器。

  
  
  1. public class BootCompleteReceiver extends BroadcastReceiver {
  2.    public BootCompleteReceiver() {
  3.    }
  4.    @Override
  5.    public void onReceive(Context context, Intent intent) {
  6.        Toast.makeText(context, "启动成功", Toast.LENGTH_SHORT).show();
  7.    }
  8. }

在AndroidMainfest文件中会自动注册接收器,然后配置权限,接收广播类型:

  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="top.xiexiaodong.broadcastdemo2">
  4.    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  5.    <application
  6.        android:allowBackup="true"
  7.        android:icon="@mipmap/ic_launcher"
  8.        android:label="@string/app_name"
  9.        android:supportsRtl="true"
  10.        android:theme="@style/AppTheme">
  11.        <activity android:name=".MainActivity">
  12.            <intent-filter>
  13.                <action android:name="android.intent.action.MAIN" />
  14.                <category android:name="android.intent.category.LAUNCHER" />
  15.            </intent-filter>
  16.        </activity>
  17.        <receiver
  18.            android:name=".BootCompleteReceiver"
  19.            android:enabled="true"
  20.            android:exported="true">
  21.            <intent-filter>
  22.                <action android:name="android.intent.action.BOOT_COMPLETED" />
  23.            </intent-filter>
  24.        </receiver>
  25.    </application>
  26. </manifest>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值