我创建了一个应用程序,如果收到SCREEN_ON意向,它将屏幕亮度设置为全屏。我使用这个应用程式是因为手机的萤幕有点破损,如果开启萤幕有时会出现一些问题。我的应用程序运行良好,但随机运行不正常。即使我的服务仍在后台运行,屏幕的亮度也不会改变。我创建了一个备份,以便在更改飞行模式时调用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()(...)
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");
}
}
最后一个清单:
<?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>
任何帮助将不胜感激。预先感谢您的回答!
答案 0 :(得分:0)
在我添加了“快速设置图块”后,问题消失了。使用此图块,我可以运行“ changeBrightness”方法,而无需使用飞行模式图块。我猜该系统杀死了我的服务时间,但是现在我的程序绑定了一个图块,因此它不再能做到这一点。
我仍然想听听有关该主题的任何想法。也许有一天我可以摆脱瓷砖。