简体   繁体   English

广播接收器未检测到关键事件

[英]Broadcast receiver not detecting keyevents

I am trying to receive Power button events in my app. 我正在尝试在我的应用中接收电源按钮事件。 I have added the following code in my Manifest file and then displaying a toast in receive method of the broadcast receiver class but still the code is not working. 我已经在清单文件中添加了以下代码,然后在广播接收器类的receive方法中显示了吐司,但是该代码仍然无法正常工作。 Am I missing something? 我想念什么吗?

 <receiver android:name="com.xxxxx">
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF"></action>
        <action android:name="android.intent.action.SCREEN_ON"></action>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
         <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
    </intent-filter>
</receiver>

Regards 问候

yeah what you want to do is possible just few things I want to clear out, showing toast while screen is off is no use also detecting power button and screen state is the same and I'll show you how to detect screen state and show toast only, but the power button I still don't know the way to detect it since power button is only managed by System, I think you'll need root to adjust or add functions to power button. 是的,您可能想做的只是几件事,我想清除,在屏幕关闭时显示烤面包是没有用的,同时检测电源按钮和屏幕状态是否相同,我将向您展示如何检测屏幕状态和显示烤面包仅,但是电源按钮我仍然不知道如何检测它,因为电源按钮仅由系统管理,我认为您需要root才能调整电源按钮或向电源按钮添加功能。 any way here is the code you'll need: 这里是您需要的任何代码:

1 - you'll need an activity that can start/stop detecting screen state, a service that keeps your app working in background, lastly the Broadcastreceiver. 1-您需要一项可以启动/停止检测屏幕状态的活动,一项使您的应用在后台运行的服务,最后是Broadcastreceiver。 so from the activity you should start service : 因此,从活动中您应该开始服务:

startService(new Intent(MainActivity.this, yourservice.class));

make sure to stop it after you are done 确保完成后将其停止

stopService(new Intent(MainActivity.this, yourservice.class));

2- here how your service should look like : 2-您的服务应如下所示:

public class yourservice extends Service{
private BroadcastReceiver mReceiver;

public void onCreate() {
    super.onCreate();
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    this.mReceiver = new your receiver();
    this.registerReceiver(this.mReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (yourbroasdcast.screenOff) {
            //here screen is off do your stuff
    } else {
            //here screen is on do your stuff example your toast
Toast.makeText(getApplicationContext(), "write your toast here", Toast.LENGTH_LONG).show();
    }

    return START_STICKY;
}

3 - your broadcasrtreceiver : 3-您的广泛接收者:

public class yourbroadcast extends BroadcastReceiver{

public static boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        screenOff = true;
    }else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        screenOff = false;
    }
    Intent i = new Intent(context, yourservice.class);
    i.putExtra("screen state", screenOff);
    context.startService(i);
}

} }

4 - the manifest should look like this 4-清单应如下所示

<service
        android:enabled="true"
        android:name=".yourservice"/>
    <receiver android:name=".yourbroadcast" android:enabled="true">  
    <intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF" />
        <action android:name="android.intent.action.SCREEN_ON" />
    </intent-filter>
    </receiver>

hope I helped... if you needed anything just ask me :) 希望我能帮助您...如果您需要什么,请问我:)

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

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