Android 来自其他活动的停止服务不工作?
我知道在这方面已经有很多问题,但它似乎不起作用,所以基本上我有一个自定义服务,当其中的计时器过期时,该服务可以正常工作,并调用stopservice方法。但是,当我尝试从另一个活动停止服务时,它不起作用 自定义服务代码:Android 来自其他活动的停止服务不工作?,android,service,android-activity,Android,Service,Android Activity,我知道在这方面已经有很多问题,但它似乎不起作用,所以基本上我有一个自定义服务,当其中的计时器过期时,该服务可以正常工作,并调用stopservice方法。但是,当我尝试从另一个活动停止服务时,它不起作用 自定义服务代码: public class ConnectionService extends Service { Intent service_intent; Timer t; int expiry_time = 0; int timer_tick_starter = 0; @Over
public class ConnectionService extends Service {
Intent service_intent;
Timer t;
int expiry_time = 0;
int timer_tick_starter = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
t = new Timer();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
service_intent = intent;
Log.d("onConnectionService", "onStartCommand");
expiry_time = Integer.parseInt(intent.getStringExtra("expiry"));
//convert the minutes to milliseconds
timer_tick_starter = (expiry_time * 60000);
Log.d("onConnectionService", "Expiry Time (in milliseconds) " + timer_tick_starter);
} catch (Exception e) {
Log.d("onConnectionService", e.getMessage().toString());
}
//timer will only start to run on the expiry_time which will then close the connection
t.schedule(new TimerTask() {
@Override
public void run() {
Log.d("onConnectionService", "inside expiry time");
//close the connection
stopService(service_intent);
}
}, 30000);
return START_NOT_STICKY;
}
@Override
public boolean stopService(Intent name) {
Log.d("onConnectionService", "StopService called");
String message = name.getStringExtra("msg");
//broadcast sender
Intent intent = new Intent();
intent.putExtra("msg", message);
intent.setAction("fi.android.inetkeyapplication.CUSTOM_INTENT");
intent.putExtra("is_selected", UserConnectionState.getInstance().is_selected);
sendBroadcast(intent);
Log.d("onConnectionService", "Service has been killed");
return super.stopService(name);
}
@Override
public void onDestroy() {
Log.d("onConnectionService", "Destroy called");
}
}
但是,在我的应用程序中,当我试图从启动它的活动中杀死它时,它不起作用:
Button exit_button = (Button) findViewById(R.id.btnExit);
connect_button.setTypeface(roboto_light);
exit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do closing call
response_close = "";
//need to close service
//also kill the service
Log.d("onConfigurationActivity", "killing service");
Intent intent = new Intent(context, ConnectionService.class);
intent.putExtra("msg", "You have been disconnected");
stopService(intent);
}
});
我已经尝试了我的class-ConfigurationActivity.this(作为上下文),并且在我使用自定义上下文的那一刻,我已经尝试了(这个),我只是在创建时进入了自定义上下文。然而,似乎没有什么工作我错过了什么
thanx好,显然停止服务并不会停止计时器。所以我刚刚在计时器上调用了cancel()接受您的答案,这样Q就不会出现在“Unanswered”部分
Button exit_button = (Button) findViewById(R.id.btnExit);
connect_button.setTypeface(roboto_light);
exit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do closing call
response_close = "";
//need to close service
//also kill the service
Log.d("onConfigurationActivity", "killing service");
Intent intent = new Intent(context, ConnectionService.class);
intent.putExtra("msg", "You have been disconnected");
stopService(intent);
}
});