广播接收器不显示通知

broad cast receive does not show notifications

我正在尝试在屏幕打开或关闭时接收通知。我注册了一个广播接收器,如下所示。

但是当我按下边缘右上角的按钮时,接收器被调用但是代码中显示的日志语句没有显示。

知道的请告诉我改正

代码:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();

    switch (action) {

    case Intent.ACTION_SCREEN_ON:
        Log.w(TAG, SubTag.msg("onReceive", "Intent.ACTION_SCREEN_ON"));
        break;
    case Intent.ACTION_SCREEN_OFF:
        Log.w(TAG, SubTag.msg("onReceive", "Intent.ACTION_SCREEN_OFF"));
        break;

    default:
        Log.w(TAG, SubTag.msg("onReceive", "UNHANDLED CASE"));
        break;
    }
}

更新:

我在onstart中注册了如下接收器:

registerReceiver(this.mScreenReceiver, new IntentFilter(intent.ACTION_SCREEN_ON))

registerReceiver(this.mScreenReceiver, new IntentFilter(intent.ACTION_SCREEN_OFF))

这不是权限,您应该在清单中注册您的广播。在清单的 application 标签内,编写此代码,但首先将名称更改为您的广播 class 名称

<receiver android:name=".your_class_name_here" >
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF" />
        <action android:name="android.intent.action.SCREEN_ON" />
    </intent-filter>
</receiver>

更新 试试这个广播

private BroadcastReceiver ScreenActions = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("BroadcastReceiver", "Broadcast is called");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.i("BroadcastReceiver", "Screen ON");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("BroadcastReceiver", "Screen OFF");
        }

    }
};

并在您的 onStart() 中注册它

registerReceiver(ScreenActions, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(ScreenActions, new IntentFilter(Intent.ACTION_SCREEN_OFF));