【问题标题】:BroadcastReceiver for Screen On/Off not working屏幕开/关的广播接收器不起作用
【发布时间】:2013-04-23 04:37:30
【问题描述】:

我正在尝试使用 BroadcastReceiver 但它不起作用,请帮我解决这个问题。 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

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

【问题讨论】:

标签: android broadcastreceiver android-broadcast


【解决方案1】:

嘿尝试使用广播的动态调用,我试过这个它会很好地工作......

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

【讨论】:

  • 是的,它正在工作,但我希望它在关闭我的应用程序后还能工作,因为我正在制作锁定屏幕,所以你有什么想法吗?
  • 你在“服务”中调用这个广播
  • kk thnx 我做到了,但是当我关闭应用程序时 n 尝试在 n 上屏蔽 n 时它不起作用所以我检查手机上正在运行的服务 n 显示我没有提供任何服务,所以你能帮忙 4 吗?
  • kk thnx 现在服务在后台运行 bt 我想在屏幕打开时打开活动,bt id 那个活动已经在运行 thn 它不能再次运行 n 显示旧的打开活动,我该怎么做你能帮我 4 分吗?
  • 我的应用不包含任何服务
【解决方案2】:

如果您希望系统调用此接收器,则需要将其导出。您设置了exported =“false”,将其更改为true 或完全删除exported,这将开始工作。通常这是不安全的,但由于 SCREEN_ON 和 SCREEN_OFF 都是受保护的广播,并且您验证操作,只有更受信任的系统代码才能将它们发送给您,所以它非常安全。

遗憾的是,这在这种情况下不起作用,因为意图广播具有以下标志: Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND

【讨论】:

    【解决方案3】:

    您可以尝试获取电池值吗:

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

    【讨论】:

    • 是的,它正在工作,但我希望它在关闭我的应用程序后还能工作,因为我正在制作锁定屏幕,所以你有什么想法吗?
    • 你在“服务”中调用这个广播
    猜你喜欢
    • 2012-03-17
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多