android - 屏幕开/关的 BroadcastReceiver 不起作用

标签 android broadcastreceiver android-broadcast

我正在尝试使用 BroadcastReceiver 但它不起作用,请帮我解决这个问题。 我的接收器.java

package com.example.broadcast_receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("[BroadcastReceiver]", "MyReceiver");

        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");
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcast_receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyReceiver" 
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON"/>
                <action android:name="android.intent.action.SCREEN_OFF"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.broadcast_receiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

BroadcastReceiver 不工作,不做任何记录,请帮我解决这个问题。

最佳答案

嘿,尝试使用广播的动态调用,我试过了,它肯定会工作...

public class MainActivity extends Activity {

    //Create broadcast object
    BroadcastReceiver mybroadcast = new BroadcastReceiver() {    
        //When Event is published, onReceive method is called
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("[BroadcastReceiver]", "MyReceiver");

            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");
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }
}

关于android - 屏幕开/关的 BroadcastReceiver 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161062/

相关文章:

android - 如何在代码中创建和注册 BroadcastReceiver

android - 计算 Spannable String 宽度

android - Windows Phone 8 等同于 Android 的 StartActivityForResult

android - add 方法如何在 kotlin 中对 List<E> type val 起作用

android - 接收 GCM 消息会消耗多少电量?

android - BroadcastReceiver.onReceive 中的上下文是否始终是调用 registerReceiver 的 Activity ?

android - 广播接收器 - 调用者验证

java - 手动终止应用程序时如何获取广播接收器?

android - 从广播事件切换到奥托有什么好处

android - 使用Android和OpenCV降低相机的FPS