【发布时间】:2018-06-02 21:07:29
【问题描述】:
我在网上搜索了很多时间,但我不明白为什么我的自定义广播 不工作。
<receiver
android:name=".myservice.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
当我重新连接并断开充电器时,它没有收到。
我这样做是为了让事情变得简单
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"Battery", Toast.LENGTH_SHORT).show();
Log.i("Recive", "Yes");
}
}
【问题讨论】:
-
你的 logcat 不打印 Log.i("Recive", "Yes");? (过滤 logcat 以显示信息消息)
-
您使用的是什么设备和安卓版本?
-
它没有打印,我使用的是 oneplus 3t android 8
-
您使用哪个 targetSdkVersion?我的答案适用于 targetSdkVersion 25 或更低版本的 Android 8 (Nexus 5X)。没有使用 targetSdkVersion 26 或更高版本对其进行测试。
-
我的手机是sdk 26
标签: android broadcastreceiver android-8.0-oreo