2015-08-29 38 views
0

我想从我的MainActivityTrackingService类通过上下文来检查onDestroy()里面的方法isFinishing()在TrackingService但我正在逐渐The method isFinishing() is undefined for the type Context获取isFinishing服务

我怎样才能得到isFinishing()内工作的内部工作服务?

MainActivity

public class MainActivity extends ActionBarActivity implements 
     AsyncTaskCallback { 
    private static Context mContext; 

    public static Context getContext() { 
      return mContext; 
     } 

} 

TrackingService:

public class TrackingService extends Service implements AsyncTaskCallback, 
     LocationListener { 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(lm != null && MainActivity.getContext().){ 
      lm.removeUpdates(this); 
      System.out.println("ABC TrackingService lm was removed."); 
     }else{ 
      System.out.println("ABC TrackingService lm locationManager is null."); 

     }  
    } 

} 

编辑

我想,当应用程序被关闭删除我TrackingService类的locationManger和不当用户切换活动

有没有办法在onDestroy()被调用时区分两种情况?

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

我想去掉两个locationManager箱子:

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

我如何能区分的onDestroy是否被调用的原因用户cloeses应用程序或当它被调用的活动之间切换的时候用户?

我很感激任何帮助。

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!"); 

    } 
} 
+0

'isFinishing()'是'Activity'而不是'Context'的一种方法。伊莫,把它称为服务没有任何意义。你想用'isFinishing'来检查什么? –

+0

@SaschaKolberg:我想检查它以防止用户从MainActivity切换到Map活动时删除locationManager。 locationManger必须在用户关闭应用程序时被删除。有没有其他方法可以做到这一点? –

+1

为什么您在切换活动时会停止服务? –

回答

0

isFinishing()只会告诉你是否没有完成活动,而不是刚刚暂停。如果您的服务在活动isFinishing()仍然可能会返回false之前被销毁,尽管应用程序已结束。

您的Service不会因为您切换到其他活动而被销毁。它应该独立于您的活动运行,直到您停止它或系统释放资源为止。如果您想防止系统倾销您的服务,您可以在其onCreate中返回START_STICKY

如果你想杀人,当应用程序被用户终止服务,可在您的主要活动的onDestroy方法停止它并没有检查活动是否已完成:

public class MainActivity extends ActionBarActivity implements 
    AsyncTaskCallback { 

    ... 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     if (isFinishing()) { 
      Intent i = new Intent(this, TrackingService.class); 
      stopService(i); 
     } 
    } 

编辑:

澄清了一些关于你想点什么之后,这里是一个可能为你工作的另一种选择:

您只需根据m开始的服务ain活动。你永远不会在你的代码中的任何地方拨打stopService。但是,您bind服务的这两个活动和undbind在每个活动的onDestroy方法。请参阅android documentation。当所有绑定已经释放时,服务应该停止。

+0

好的,我有更多的文字和代码给probelm。请看看它。 –

+0

@MrAsker更新了我的回答 –

+0

我曾尝试过。 MainActivity中的onDestroy()仅在关闭应用程序时调用。当我从MainActivity转到Map Activity时(服务仍然有效,服务onDestroy()未被调用),TrackingService类中的'onDestroy()'被调用。当我返回时(服务仍然有效),但是当我从MainActivity再次返回Map时,由于调用了TrackingService的onDestroy(),服务被销毁并且locationManager被移除。 –