BroadcastReceiver的对屏幕开/关不工作 [英] BroadcastReceiver for Screen On/Off not working

查看:105
本文介绍了BroadcastReceiver的对屏幕开/关不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的BroadcastReceiver,但它不能正常工作,请帮我解决这个问题。 MyReceiver.java

I am trying to use BroadcastReceiver but it is not working, please help me to solve this problem. MyReceiver.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中

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没有工作,没有做任何日志,请帮我解决这个问题。

BroadcastReceiver not working and not making any log, please help me to solve this problem.

推荐答案

嘿尝试使用广播动态调用,我想这将切切实实的工作......

Hey try using dynamic calling of broadcast,I tried this it will surly work...

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

这篇关于BroadcastReceiver的对屏幕开/关不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆