0

我使用Google地图创建了一个应用程序来检查我的位置,并在我接近标记时提醒我。如何停止应用程序关闭后启动的服务 - android

如果我关闭了这个应用程序,我创建了一个服务,从我的主要活动开始在OnDestroy()方法中。该服务启动并执行得非常好。但是当我再次打开应用程序时,我需要停止此服务,所以我把停止服务(意图)放在OnCreate方法中。但该服务并没有停止,并不断向我发送通知。

我的服务:

public class ServiceClass extends Service{ 
private ArrayList<Prod> est = new ArrayList<Prod>(); 
private int i = 0; 
private float[] distance = new float[2]; 

private LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     i = 0; 
     while (i < est.size()){ 
      Location.distanceBetween(location.getLatitude(), location.getLongitude(), est.get(i).getLocation().latitude, est.get(i).getLocation().longitude, distance); 
      if (distance[0] > est.get(i).getRange()) { 

      } else { 
       Toast.makeText(ServiceClass.this, "in circle"+i, Toast.LENGTH_SHORT).show(); 

       NotificationCompat.Builder mBuilder = 
         new NotificationCompat.Builder(ServiceClass.this) 
           .setSmallIcon(R.mipmap.ic_launcher) 
           .setContentTitle("Distance") 
           .setContentText("Test notification"); 
       NotificationManager mNotificationManager = 
         (NotificationManager) getSystemService(
           Context.NOTIFICATION_SERVICE); 
       mNotificationManager.notify(1, mBuilder.build()); 

       Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
       Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
       r.play(); 
      } 
      i++; 
     } 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 
}; 

public ServiceClass() { 
} 


@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    GetLocation get = new GetLocation(); 
    get.execute(); 
    Toast.makeText(ServiceClass.this, "Service started", Toast.LENGTH_SHORT).show(); 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    locationManager.removeUpdates(locationListener); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    return START_STICKY; 
} 

在我MapsActivity我在开始的onDestroy服务:

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Intent intent = new Intent(this, ServiceClass.class); 
    startService(intent); 
} 

这是工作。该应用程序已关闭,服务已启动,并在我的位置靠近时显示通知。


问题是,当我再次打开应用程序时,我需要取消服务,以便在应用程序打开时不显示通知。

但是没有工作。

我叫stopService在我的onCreate:

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    Intent intent = new Intent(this, ServiceClass.class); 
    stopService(intent); 
} 

不要工作,服务继续向我发送通知。

我在清单中声明的​​服务:

<service 
    android:name=".Controller.ServiceClass" 
    android:enabled="true" 
    android:exported="false" > 
</service> 

回答

1

你可以尝试重写ServiceonDestroy()方法,并从LocationManager取出位置监听器。您还应该取消通知。

下面的代码添加到您的服务:

@Override 
public void onDestroy() { 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    locationManager.removeUpdates(locationListener); 

    NotificationManager mNotificationManager = 
        (NotificationManager) getSystemService(
          Context.NOTIFICATION_SERVICE); 
    mNotificationManager.cancel(1); 
} 
+0

让我看看我的理解。从我的ServiceClass中覆盖一个onDestroy方法,用位置管理器删除位置侦听器?我怎么样?就像我在我的地图活动中声明onDestroy一样? – FelipeRsN

+0

刚刚更新了您的服务代码的答案。 –

+0

谢谢你的帮助。我尝试过并努力工作。 – FelipeRsN

相关问题