关闭应用程序时停止服务,而不是在用户更改活动时停止服务

时间:2015-08-29 10:56:42

标签: android

我有一个trackingService locationManager组件,该组件在后台处理,并从MainActivity活动启动。与此同时,我有另一个组件从服务器检索此数据并将其显示在Map活动中。当用户单击其中的按钮时,用户可以从MainActivity访问服务器中的数据。于是,正在从服务器检索数据,用户重定向到Map活动。到目前为止,它对我有用。

我试图在两种情况下删除locationManager

  • 当用户点击MainActivity菜单中的复选框时。
  • 或关闭应用时(非用户更改活动时)。

使用此代码段:

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(wifi_receiver);
    System.out.println("ABC TrackingService onDestroy() was invoked.");
    // commeted 13.08.15

    if(lm != null){
        lm.removeUpdates(this);
        System.out.println("ABC lm locationManager is not equal null.");
    }else{
        System.out.println("ABC lm locationManager is null.");

    }       
}

我已尝试使用下面的代码,因此当我第一次单击MainActivity中的按钮时,用户将被重定向到Map活动并且服务正常工作(不调用{{1}在onDestroy()类中)但是当我从Map活动返回到MainActivity(服务仍然有效),然后点击按钮再次转到TrackingService活动时,此时{正在调用Map类中的{1}},服务停止

使用此代码段:

onDestroy()

我收到以下错误: 的错误:

TrackingService

如何在这两种情况下停止服务,以及为什么第一次点击按钮时没有调用onDestroy()?

我感谢任何帮助。

MainActivity:

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(wifi_receiver);
    System.out.println("ABC TrackingService onDestroy() was invoked.");
    lm.removeUpdates(this);

}

TrackingService类:

08-29 12:46:06.624: E/AndroidRuntime(6332): FATAL EXCEPTION: main
08-29 12:46:06.624: E/AndroidRuntime(6332): Process: com.bustracker, PID: 6332
08-29 12:46:06.624: E/AndroidRuntime(6332): java.lang.RuntimeException: Unable to stop service com.bustracker.TrackingService@1e57ec95: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.removeUpdates(android.location.LocationListener)' on a null object reference
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3356)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.app.ActivityThread.access$2300(ActivityThread.java:177)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1551)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.os.Looper.loop(Looper.java:145)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.app.ActivityThread.main(ActivityThread.java:5944)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at java.lang.reflect.Method.invoke(Native Method)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at java.lang.reflect.Method.invoke(Method.java:372)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
08-29 12:46:06.624: E/AndroidRuntime(6332): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.removeUpdates(android.location.LocationListener)' on a null object reference
08-29 12:46:06.624: E/AndroidRuntime(6332):     at com.bustracker.TrackingService.onDestroy(TrackingService.java:301)
08-29 12:46:06.624: E/AndroidRuntime(6332):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3339)
08-29 12:46:06.624: E/AndroidRuntime(6332):     ... 9 more

地图活动:

public class MainActivity extends ActionBarActivity implements
        AsyncTaskCallback {
    TrackingService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.route_available);
        // Start the TrackingService class.
        Intent i = new Intent(this, TrackingService.class);
        startService(i);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        System.out.println("test onCreateOptionsMenu was invoked.");

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem checkable = menu.findItem(R.id.checkable_menu);
        checkable.setChecked(isChecked);
        return true;
    }

    // Start and stop the background service.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.checkable_menu:
            if (isChecked = !item.isChecked()) {
                item.setChecked(isChecked);
                Intent i = new Intent(this, TrackingService.class);
                startService(i);
                System.out.println("test if onOptionsItemSelected");
            } else {
                mService.stopTrackingService();

            }
            return true;

        default:
            return false;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

MainActivity中尝试类似:

@Override
public void onBackPressed() {
    super.onBackPressed();
    // remove locationManager here
    finish();
}