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

Broadcastreceiver on Android 7.1.1 sometimes not working

我创建了一个应用程序,如果收到 SCREEN_ON 意图,它会将屏幕亮度设置为全亮度。我使用这个应用程序是因为我的 phone 屏幕有点坏,如果打开屏幕有时会出现一些问题。我的应用程序运行良好,只是随机它不起作用。即使我的服务还在后台运行,但是屏幕的亮度并没有改变。我创建了一个备份,这样如果改变飞行模式,就会调用 changeBrightness () 方法。它总是有效。我想这个问题与 IntentFilter 有关,因为那是 SCREEN_ON 意图和 AIRPLANEMODE_CHANGED 意图之间的区别。

我的后台服务:

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()(...)

广播接收器:

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

}

最后是清单:

<?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>

如有任何帮助,我们将不胜感激。提前感谢您的回答!

在我向我的应用程序添加 "quick settings tile" 之后,问题就消失了。有了这个图块,我可以 运行 我的 "changeBrightness" 方法,而无需使用飞行模式图块。我想系统有时会终止我的服务,但现在我有一个绑定到我的程序的磁贴,它不能再这样做了。

我仍然想听听有关该主题的任何想法。也许有一天我可以摆脱瓷砖。