Android:事件ACTION_POWER_CONNECTED未发送到我的BroadcastReceiver

fhu*_*cho 7 android android-intent

手机充电后我想做些什么.所以我创建了ChargingOnReciever:

public class ChargingOnReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
        context.startActivity(someActivity);
        Log.d(TAG, "Phone was connected to power");
    } 
} 
Run Code Online (Sandbox Code Playgroud)

我希望我的接收器听android.intent.action.ACTION_POWER_CONNECTED,所以我把它放到显而易见的地方:

<reciever android:name=".ChargingOnReceiver"
          android:enabled="true"
          android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
    </intent-filter>
</reciever>
Run Code Online (Sandbox Code Playgroud)

但是ChargingOnReceiver当我把G1放到充电器(通过USB线连接到我的笔记本电脑)时,显然没有启动.任何帮助深表感谢.

fhu*_*cho 26

它是接收器,而不是接收器!我花了5个小时才发现这个愚蠢的错误.我认为Android Eclipse插件应该在清单xml中进行一些语法检查.


Abh*_*hra 7

对于任何试图为 "android.intent.action.ACTION_POWER_CONNECTED" 和 "android.intent.action.ACTION_POWER_DISCONNECTED" 注册接收器的人,我想添加:

作为 Android 8.0(API 级别 26)后台执行限制的一部分,面向 API 级别 26 或更高级别的应用无法再在其清单中为隐式广播注册广播接收器。但是,目前有几个广播不受这些限制。应用程序可以继续为豁免广播注册侦听器,无论应用程序的目标 API 级别是什么。

以上两个广播不再在这些豁免广播的列表中。请参考以下文档:

https://developer.android.com/guide/components/broadcast-exceptions