英文:
How to make service continues to work after the app is restarted
问题
我有一个通过按按钮工作的服务。
内部有一个每秒工作一次的计数器。
我测试过它,它在工作。
即使在应用程序关闭后它也在工作。
问题出现在应用程序关闭后,
然后我执行一个运行
测试该服务,它不工作。
我如何在从Android Studio内部运行后使它工作?
// MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isMyServiceRunning(SensorService.class)) {
Log.i("isServiceRunning", "Not");
} else {
Log.i("isServiceRunning", "Don");
}
}
public void Test(View view) {
startService(new Intent(this, SensorService.class));
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
/// Service
public class SensorService extends Service {
public int counter = 0;
public SensorService() {
super();
Log.i("HERE", "here I am!");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
// Intent broadcastIntent = new Intent(this, SensorRestarterBroadcastReceiver.class);
stoptimertask();
}
private Timer timer;
private TimerTask timerTask;
long oldTime = 0;
public void startTimer() {
// set a new Timer
Log.i("startTimer", "Don");
timer = new Timer();
// initialize the TimerTask's job
initializeTimerTask();
// schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
Log.i("initializeTimerTask", "Don");
timerTask = new TimerTask() {
public void run() {
Log.i("in_timer", "in timer ++++ " + (counter++));
}
};
}
/**
* not needed
*/
public void stoptimertask() {
// stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
英文:
I have a service works by pressing a button.
There's a counter that works every second inside it.
I tested it and it's working.
It also works after the application is shut down.
The problem after the application is closed
Then I'm doing a run
test the service it doesn't work.
How do I make it works after i do run from inside Android studio ?
//MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!isMyServiceRunning(SensorService.class)){
Log.i("isServiceRunning","Not");
}else {
Log.i("isServiceRunning","Don");
}
}
public void Test(View view){
startService(new Intent(this, SensorService.class));
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
/// Service
public class SensorService extends Service {
public int counter=0;
public SensorService() {
super();
Log.i("HERE", "here I am!");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
// Intent broadcastIntent = new Intent(this, SensorRestarterBroadcastReceiver.class);
stoptimertask();
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
//set a new Timer
Log.i("startTimer", "Don");
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
Log.i("initializeTimerTask", "Don");
timerTask = new TimerTask() {
public void run() {
Log.i("in_timer", "in timer ++++ "+ (counter++));
}
};
}
/**
* not needed
*/
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent){
return null;
}
}
答案1
得分: 0
如果您在Android Studio中使用“Run”(无论是测试还是应用程序),由于整个应用程序总会首先停止,所以服务也会始终停止。如果您手动从设备上启动应用程序,则服务将不会停止。
对此无法绕过,实际上这是一个很好的功能。它确保每次运行时始终以清洁的状态启动应用程序。如果您想分享为什么想要这样做的原因,可能有一种替代方法来解决您的问题。
英文:
If you use "Run" from Android Studio (regardless if it's tests or the app) the service will always stop first since the whole application will always stop first. The service will not stop if you start the App manually from the device.
There is no way around this and is actually a good feature. It ensures that the App is always started cleanly on every run. If you want to share the reason of why you want to do this, there might be an alternate way of solving your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论