简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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