简体   繁体   English

Android 7.1.1上的Broadcastreceiver有时无法正常工作

[英]Broadcastreceiver on Android 7.1.1 sometimes not working

I've created an app that sets the screen brightness to full if the SCREEN_ON intent is received. 我创建了一个应用程序,如果收到SCREEN_ON意向,它将屏幕亮度设置为全屏。 I use this app because the screen of my phone is a bit broken and sometimes some problems occur if the screen is turned on. 我使用这个应用程式是因为手机的萤幕有点破损,如果开启萤幕有时会出现一些问题。 My app works well, except that randomly it does not work. 我的应用程序运行良好,但随机运行不正常。 Even though my service is still running in the background, the brightness of the screen is not changed. 即使我的服务仍在后台运行,屏幕的亮度也不会改变。 I created a backup so that if the airplane mode is changed, the changeBrightness () method is called. 我创建了一个备份,以便在更改飞行模式时调用changeBrightness()方法。 It always works. 它始终有效。 I guess the problem has something to do with the IntentFilter, because that's the difference between the SCREEN_ON intent and the AIRPLANEMODE_CHANGED intent. 我想这个问题与IntentFilter有关,因为那是SCREEN_ON意图和AIRPLANEMODE_CHANGED意图之间的区别。

My Background Service: 我的后台服务:

public class UpdateService extends Service {
public int counter = 0;
BroadcastReceiver screenReceiver;

public UpdateService(Context applicationContext) {
    super();
}

public UpdateService() {
}

@Override
public void onCreate(){
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.setPriority(100);
    BroadcastReceiver mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);
    screenReceiver = mReceiver;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    if (intent != null) {
        boolean screenOff = intent.getBooleanExtra("screen_state", true);

        if (screenOff == false) {
            changeBrightness();
        }
    }
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Intent broadcastIntent = new Intent("RestartSensor");
    unregisterReceiver(screenReceiver);
    sendBroadcast(broadcastIntent);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


public void changeBrightness()(...)

BroadcastReceiver: 广播接收器:

public class Receiver extends BroadcastReceiver {
private boolean screenOff;
Intent i;

@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)) {
        Log.d("Info", "Screen on, start service!");
        screenOff = false;
    } else if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
        screenOff = false;
    } else if (intent.getAction().equals("RestartService")) {
        screenOff = true;
        Log.d("Info", "RestartService received");
    }

    context.startService(new Intent(context, UpdateService.class).putExtra("screen_state", screenOff));
    screenOff = true;
    Log.d("Info", "bottom onReceive");
}

} }

Finally the manifest: 最后清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".UpdateService"
        android:enabled="true"></service>

    <receiver
        android:name=".Receiver"
        android:enabled="true"
        android:exported="true"
        android:label="RestartServiceWhenStopped">
        <intent-filter>
            <action android:name="RestartSensor" />
            <action android:name="android.intent.action.AIRPLANE_MODE" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance for any answer! 预先感谢您的任何回答!

The problem went away, after I added a "quick settings tile" to my applikation. 在我添加了“快速设置磁贴”后,问题消失了。 With this tile I could run my "changeBrightness" method without using the airplane mode tile. 有了这个图块,我就可以运行我的“ changeBrightness”方法,而无需使用飞行模式图块。 I guess the system killed my service somtimes, but now that I have a tile bound to my program, it can't do that anymore. 我猜该系统杀死了我的服务时间,但是现在我的程序绑定了一个图块,所以它不再能这样做了。

I would still like to hear any ideas on the topic. 我仍然想听听有关该主题的任何想法。 Maybe one day I can get rid of the tile. 也许有一天我可以摆脱瓷砖。

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

相关问题 有时Android BroadcastReceiver无法正常工作 - Sometimes Android BroadcastReceiver does not work Android:AlarmManager有时不调用BroadcastReceiver - Android: AlarmManager sometimes doesn't call BroadcastReceiver BroadcastReceiver android - 关闭应用程序后有时会继续,有时不会 - BroadcastReceiver android - Sometimes it continues after closing the app, sometimes not ANDROID sendBroadcast到UNITY BroadcastReceiver无效 - ANDROID sendBroadcast to UNITY BroadcastReceiver not working Android BroadcastReceiver无法正常工作(包括错误) - Android BroadcastReceiver not working (error included) Android Nougat 7.1.1 broadcastReceiver 不接收动作 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION - Android Nougat 7.1.1 broadcastReceiver don't receive the action WifiManager.SCAN_RESULTS_AVAILABLE_ACTION 其他应用程序的 Android Broadcastreceiver 安装/删除不起作用 - Android Broadcastreceiver for other apps install/delete not working Broadcastreceiver无法在android Nougat API 25级中工作吗? - Broadcastreceiver not working in android Nougat api level 25? Android BroadcastReceiver 无法通过单击通知操作来工作 - Android BroadcastReceiver not working by clicking at notification action setColorFilter有时在android drawable上不起作用 - setColorFilter is not working sometimes on android drawable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM