简体   繁体   English

屏幕打开/关闭的广播接收器不起作用

[英]BroadcastReceiver for Screen On/Off not working

I am trying to use BroadcastReceiver but it is not working, please help me to solve this problem. 我正在尝试使用BroadcastReceiver,但无法正常工作,请帮助我解决此问题。 MyReceiver.java 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 not working and not making any log, please help me to solve this problem. BroadcastReceiver无法正常工作且未进行任何日志记录,请帮助我解决此问题。

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

Can you try with getting battery value: 您可以尝试获取电池电量吗:

public class Broadcast 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) {
            //Get battery percentage
            int batterylevel = intent.getIntExtra("level", 0);    
            //get progressbar
            ProgressBar myprogressbar = (ProgressBar) findViewById(R.id.progressbar);
            myprogressbar.setProgress(batterylevel);
            TextView tv = (TextView) findViewById(R.id.textfield);
            //Set TextView with text
            tv.setText("Battery Level: " + Integer.toString(batterylevel) + "%");
        }
    });

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);   
        registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
} 

If you want this receiver to be called by the system, you would need to export it. 如果希望该接收器由系统调用,则需要将其导出。 You set exported = "false", change this to true or remove exported entirely and this will start working. 您将exported设置为“ false”,将其更改为true或完全删除exported,它将开始工作。 Normally this would be insecure, but as both SCREEN_ON and SCREEN_OFF are protected-broadcasts, and you verify the actions, only more trusted system code can send them too you, so it's pretty safe. 通常这是不安全的,但是由于SCREEN_ON和SCREEN_OFF都是受保护的广播,并且您可以验证操作,因此只有更受信任的系统代码才能向您发送这些操作,因此非常安全。

Sadly this wont work in this case as the intents broadcast have the following flags: Intent.FLAG_RECEIVER_REGISTERED_ONLY | 遗憾的是,在这种情况下,这将无法正常工作,因为广播的意图具有以下标志:Intent.FLAG_RECEIVER_register_ONLY | Intent.FLAG_RECEIVER_FOREGROUND Intent.FLAG_RECEIVER_FOREGROUND

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM