onDestroy中的变量用于区分应用程序是关闭还是用户更改活动

时间:2015-08-29 18:24:58

标签: android

我正在尝试在关闭应用时删除TrackingService类的locationManger,而不是在用户切换活动时删除

在调用onDestroy()时,是否有区分这两种情况?

我有一个trackingService locationManager组件,该组件在后台处理,并从MainActivity活动启动。与此同时,我有另一个组件从服务器检索此数据并将其显示在Map活动中。用户可以从MainActivity访问服务器中的此数据,当他单击MainActivity中的按钮时,带有InsentService类的alarmManager开始从服务器检索数据以在Map活动中显示它。

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

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

如何判断是否正在调用onDestroy导致用户欺骗应用程序,或者当用户在活动之间切换时是否调用onDestroy?

我感谢任何帮助。

MainActivity:

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;
        }
    }
@Override
protected void onDestroy() {
    super.onDestroy();
    Intent i = new Intent(this, TrackingService.class);
    stopService(i);

    }

}

TrackingService类:

public class TrackingService extends Service implements AsyncTaskCallback,
        LocationListener {
    LocationManager lm;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        detectLocation();
        return START_STICKY;
    }
    private void detectLocation() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
                .show();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                this);
        enableGPS(lm);

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

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

    }       
  }
public void stopTrackingService(){
    lm.removeUpdates(this);
  }
}

地图活动:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
    @Override
    protected void onDestroy() {
        super.onDestroy();
 // To stop the service when the user closed the app in the background and the map ativity was opened.      
        stopAlarm();
        Intent i = new Intent(this, TrackingService.class);
        stopService(i);
        System.out.println("ABC Map onDestroy() was invoked!");

    }

}

使用isFinishing进行编辑:

@Override
protected void onDestroy() {
    super.onDestroy();
    if(isFinishing()){
        Intent i = new Intent(this, TrackingService.class);
        stopService(i);
        System.out.println("ABC Map onDestroy() was invoked!" + isFinishing());

    }


}

1 个答案:

答案 0 :(得分:0)

您无法以一般方式在Activity级别区分。对于您的程序,您知道何时开始另一项活动,因此您可以自己跟踪它。

您可以保留成员变量

   boolean mOpeningMap = false;

在您的代码中,您致电startActivity,设置mOpeningMap = true,然后将onDestroy更改为 -

@Override
protected void onDestroy() {
    super.onDestroy();
    if(!mOpeningMap){
        Intent i = new Intent(this, TrackingService.class);
        stopService(i);
        System.out.println("ABC Map onDestroy() was invoked!");    
    }
}

onResume中将变量设置为false,以确保在您从MapActivity返回时重置该变量。