隐式广播接收器不调用
Implicit Broadcast Reciever isn't calling
我在网上搜索了很多时间,但我不明白为什么我的自定义广播
不工作。
<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");
}
}
来自 docs:
ACTION_BATTERY_CHANGED
Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery. See BatteryManager for documentation on the contents of the Intent.
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers
因此,您不能使用在 Manifest
中贴标的 BroadcastReceiver
,只能从您的上下文中明确注册。
此外,您的电源连接 BroadcastReceiver
似乎是正确的。尝试将其分离到另一个 BroadcastReceiver
,也许操作 ACTION_BATTERY_CHANGED
正在干扰其他操作。
这是我声明的 BroadcastReceiver
,我正在使用它,它在我的应用程序中运行。
<receiver android:name=".PowerConnectionBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
PowerConnectionBroadcastReceiver
public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PowerRcvr";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Log.d(TAG, "Device is charging");
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Log.d(TAG, "Device is NOT charging");
} else {
Log.d(TAG, "Unable to check if device is charging or not");
}
}
}
注意:此代码适用于 Android 8,targetSdkVersion 25 或更低。
在 targetSdkVersion 26 或更高版本中,由于背景限制,BroadcastReceivers
中的大部分内容无法通过 Manifest
工作。这里有 documentation(感谢 Pawel)。所以你的 IntentFilters
不会起作用。为了让它正常工作,您可以将 targetSdkVersion 下载到 25 或更低。
我在网上搜索了很多时间,但我不明白为什么我的自定义广播 不工作。
<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");
}
}
来自 docs:
ACTION_BATTERY_CHANGED Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery. See BatteryManager for documentation on the contents of the Intent.
You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). See ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED, and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts that are sent and can be received through manifest receivers
因此,您不能使用在 Manifest
中贴标的 BroadcastReceiver
,只能从您的上下文中明确注册。
此外,您的电源连接 BroadcastReceiver
似乎是正确的。尝试将其分离到另一个 BroadcastReceiver
,也许操作 ACTION_BATTERY_CHANGED
正在干扰其他操作。
这是我声明的 BroadcastReceiver
,我正在使用它,它在我的应用程序中运行。
<receiver android:name=".PowerConnectionBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
PowerConnectionBroadcastReceiver
public class PowerConnectionBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PowerRcvr";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Log.d(TAG, "Device is charging");
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Log.d(TAG, "Device is NOT charging");
} else {
Log.d(TAG, "Unable to check if device is charging or not");
}
}
}
注意:此代码适用于 Android 8,targetSdkVersion 25 或更低。
在 targetSdkVersion 26 或更高版本中,由于背景限制,BroadcastReceivers
中的大部分内容无法通过 Manifest
工作。这里有 documentation(感谢 Pawel)。所以你的 IntentFilters
不会起作用。为了让它正常工作,您可以将 targetSdkVersion 下载到 25 或更低。