【问题标题】:How to stop a service started after app is closed - android应用程序关闭后如何停止启动的服务 - android
【发布时间】:2015-11-25 12:49:23
【问题描述】:

我使用谷歌地图创建了一个应用程序来检查我的位置并在我靠近标记时提醒我。

如果我关闭此应用程序,我会创建一个服务,以便从我的主要活动中以 OnDestroy() 方法启动。该服务启动并执行得非常好。但是当我再次打开应用程序时,我需要停止此服务,所以我将 stopservice(intent) 放在 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);
}

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


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

但是没有用。

我在 onCreate 中调用 stopService:

@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>

【问题讨论】:

    标签: android android-activity service android-manifest background-process


    【解决方案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);
    }
    

    【讨论】:

    • 让我看看我是否明白。覆盖我的 ServiceClass 中的 onDestroy 方法,使用位置管理器删除位置侦听器?我该怎么做?只需像我在 mapsactivity 中那样声明 onDestroy 即可?
    • 刚刚用您的服务代码更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2015-06-02
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多