Java 停止Android上的服务
我无法使用Java 停止Android上的服务,java,android,service,Java,Android,Service,我无法使用stopService()停止我的服务。我什么都试过了,但没用。我只在卸载应用程序时停止服务 public class Refresh extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int on
stopService()
停止我的服务。我什么都试过了,但没用。我只在卸载应用程序时停止服务
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());
}
}
这是主要活动中的方法:
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());
}
}
public void startService(){
startService(new Intent(getBaseContext(), Refresh.class));
}
public void stopService(){
stopService(new Intent(getBaseContext(),Refresh.class));
}
这就是我调用启动和停止服务的方法:
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());
}
}
public void onClick(View V){
stopService();
}
您终止服务,但警报仍然设置,您必须在
onDestroy
中从警报管理器中取消挂起的意图。您终止服务,但警报仍然设置,您必须取消onDestroy中报警管理器的挂起意图我认为您用于启动服务的报警管理器正在重新启动服务,您必须停止报警,像这样停止报警
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());
}
}
Intent intent = new Intent(this, YourService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
将您在创建挂起意图时使用的挂起意图的相同ID传递给我认为用于启动服务的报警管理器正在重新启动服务,您必须停止报警,像这样停止报警
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());
}
}
Intent intent = new Intent(this, YourService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
为您在创建挂起的意图时使用的挂起的意图传递相同的ID,您必须取消AlarmManager,因此服务不会每20秒重新启动一次。
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());
}
}
之后,您可以使用stopService(),只需插入要停止的服务即可
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());
}
}
创建一个意图
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());
}
}
Intent intent = new Intent(getBaseContext(), Refresh .class);
当您要启动服务时:
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());
}
}
startService(intent);
stopService(intent);
当您要停止服务时:
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());
}
}
startService(intent);
stopService(intent);
再次,取消AlarmManager:)在调用stopService()之前执行此操作
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());
}
}
您必须取消AlarmManager,因此服务不会每20秒重新启动一次。
之后,您可以使用stopService(),只需插入要停止的服务即可
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());
}
}
创建一个意图
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());
}
}
Intent intent = new Intent(getBaseContext(), Refresh .class);
当您要启动服务时:
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());
}
}
startService(intent);
stopService(intent);
当您要停止服务时:
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());
}
}
startService(intent);
stopService(intent);
再次,取消AlarmManager:)在调用stopService()之前执行此操作
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());
}
}
警报可以重新启动服务。您应该取消它。警报可以重新启动服务。您应该取消它。问题是因为您的闹钟。您的服务已被破坏,但您挂起的报警意图没有取消。问题是因为您的报警。您的服务已被破坏,但您的未决报警意图并未取消。
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());
}
}