[英]Stop service on Android
I can't stop my service with stopService()
. 我无法使用
stopService()
停止服务。 I have tryed everything but it doesn't work. 我已经尝试了一切,但是没有用。 I stop my service only when I uninstall app.
仅当我卸载应用程序时,我才停止服务。
public class Refresh extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "NESTO!", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Your location is: "+MyLocation(), Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
@Override
public void onCreate() {
Intent myService = new Intent(this, Refresh.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0,myService, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 20* 1000, pendingIntent);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public LatLng MyLocation(){
LocationManager locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return new LatLng(location.getLatitude(), location.getLongitude());
}
}
And this is methods in main activity: 这是主要活动中的方法:
public void startService(){
startService(new Intent(getBaseContext(), Refresh.class));
}
public void stopService(){
stopService(new Intent(getBaseContext(),Refresh.class));
}
And this is how I call methods start and stop service: 这就是我所谓的方法启动和停止服务的方式:
public void onClick(View V){
stopService();
}
您终止了该服务,但仍设置了警报,您必须在onDestroy
从警报管理器中取消挂起的意图
I think the alarm manager
you are using to start the service
is restarting the service
for you,You have to stop
that alarm
,Stop the alarm like this 我认为您用来启动
service
的alarm manager
正在为您重新启动service
,您必须stop
该alarm
,像这样停止警报
Intent intent = new Intent(this, YourService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pass the SAME ID FOR THE PENDING INTENT which you used while creating the pending intent 传递您在创建待定意图时使用的待定意图的相同ID
You have to cancel your AlarmManager, so service will not restart every 20sec. 您必须取消AlarmManager,因此服务不会每20秒重新启动一次。 After that you can use stopService() just please insert what service you want to stop.
之后,您可以使用stopService(),只需插入要停止的服务即可。
Create a intent... 创建意图...
Intent intent = new Intent(getBaseContext(), Refresh .class);
When you want to start the service: 当您要启动服务时:
startService(intent);
When you want to stop the service: 当您想要停止服务时:
stopService(intent);
and again, cancel your AlarmManager :) do it before you call stopService(); 再次取消您的AlarmManager :)在调用stopService()之前执行此操作;
alarm.cancel(pendingIntent);
Alarm can restart the service. 警报可以重新启动服务。 You should cancel it.
您应该取消它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.