如何更改服务的首选项?
How can I change the preferences of a service?
我正在制作一个我想在开机时启动并 运行 在后台运行的应用程序。我决定按照本教程将其作为一项服务:
Android - Start service on boot
但是,我希望用户能够打开该应用程序并按下按钮以 enable/disable 它的功能。我有一个名为 enabled 的布尔值,我正在使用 SharedPreferences onStop 和 onStart:
保存它
//Save preferences on stop
@Override
public void onStop() {
super.onStop();
SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("AppEnabled", enabled);
editor.commit();
}
//Load preferences on start
@Override
public void onStart() {
super.onStart();
SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
enabled = pref.getBoolean("AppEnabled", true);
//Make button reflect saved preference
Button button = (Button)findViewById(R.id.enableButton);
if(enabled) {
button.setText("Disable");
}
else {
button.setText("Enable");
}
}
如果我打开应用程序并单击按钮,功能会根据需要切换。但是,如果我单击按钮禁用该功能并关闭应用程序,后台服务 运行 仍然认为它已启用。我怎样才能正确更新服务以使其获得更新的变量?
编辑:
这在清单中注册并在启动时调用:
/*This class starts MainService on boot*/
package com.example.sayonara;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
public class StartAppServiceOnBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, MainService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
Log.i("Autostart", "started");
}
}
上面的class调用这个来启动服务:
/*Called by StartAppServiceOnBoot, starts mainActivity as a service*/
package com.example.sayonara;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MainService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(), MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
事实证明,由于我正在扩展 BroadCastReceiver,class 在后台 运行 并且独立于我的应用程序,这就是为什么它即使在服务关闭时也会阻止调用。我遵循此 tutorial 以便在服务关闭时禁用我的接收器并且现在可以使用。
我正在制作一个我想在开机时启动并 运行 在后台运行的应用程序。我决定按照本教程将其作为一项服务:
Android - Start service on boot
但是,我希望用户能够打开该应用程序并按下按钮以 enable/disable 它的功能。我有一个名为 enabled 的布尔值,我正在使用 SharedPreferences onStop 和 onStart:
保存它//Save preferences on stop
@Override
public void onStop() {
super.onStop();
SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("AppEnabled", enabled);
editor.commit();
}
//Load preferences on start
@Override
public void onStart() {
super.onStart();
SharedPreferences pref = getSharedPreferences("info", MODE_PRIVATE);
enabled = pref.getBoolean("AppEnabled", true);
//Make button reflect saved preference
Button button = (Button)findViewById(R.id.enableButton);
if(enabled) {
button.setText("Disable");
}
else {
button.setText("Enable");
}
}
如果我打开应用程序并单击按钮,功能会根据需要切换。但是,如果我单击按钮禁用该功能并关闭应用程序,后台服务 运行 仍然认为它已启用。我怎样才能正确更新服务以使其获得更新的变量?
编辑:
这在清单中注册并在启动时调用:
/*This class starts MainService on boot*/
package com.example.sayonara;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
public class StartAppServiceOnBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, MainService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
Log.i("Autostart", "started");
}
}
上面的class调用这个来启动服务:
/*Called by StartAppServiceOnBoot, starts mainActivity as a service*/
package com.example.sayonara;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MainService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(), MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
事实证明,由于我正在扩展 BroadCastReceiver,class 在后台 运行 并且独立于我的应用程序,这就是为什么它即使在服务关闭时也会阻止调用。我遵循此 tutorial 以便在服务关闭时禁用我的接收器并且现在可以使用。