2017-03-08 55 views
0

我想在我的應用程序中訪問gps服務。因此,我在按鈕的onClick()方法內使用了Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);。調用Intent後,我啓動服務以偵聽位置更新。在onResume方法中,我檢查gps服務。如果它被禁用,我停止服務。但是,當顯示位置設置並且在用戶打開gps之前服務最終停止時,會調用onResume方法。Android onResume方法在活動不可見時調用

代碼:

 Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(i); 
    gps_intent=new Intent(AddOffers.this, StatsGpsService.class); 
    gps_intent.putExtra("offr_act_ind",true); 
    startService(gps_intent); 



@Override 
    protected void onResume() { 
    super.onResume(); 
    LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    boolean isproviderenabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    if(isproviderenabled==false){ 
     if(gps_intent!=null){ 
      Log.e("g","g"); 
     stopService(gps_intent); 
    }} 

StatsGpsService.java:

public class StatsGpsService extends Service { 
    LocationListener locationListener; 
    LocationManager locationManager; 
    boolean flag1; 

    public StatsGpsService() { 
     super(); 

    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.e("Service","Created"); 


    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     handleLocation(intent); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.e("Service","Destroyed"); 
    } 

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

    public void handleLocation(Intent intent){ 
     if(intent!= null) { 
      flag1 = intent.getBooleanExtra("offr_act_ind", false); 

      locationListener = new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        if (flag1 == true) { 

         Intent intent = new Intent("loc_updts"); 
         intent.putExtra("stat_lat", location.getLatitude()); 
         intent.putExtra("stat_longt", location.getLongitude()); 
         sendBroadcast(intent); 
        } 
       } 

       @Override 
       public void onStatusChanged(String s, int i, Bundle bundle) { 

       } 

       @Override 
       public void onProviderEnabled(String s) { 

       } 

       @Override 
       public void onProviderDisabled(String s) { 
        Log.e("Provider","Disabled"); 

       } 
      }; 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 


      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 
     } 
    } 
} 
+0

在哪個回調的活動中,你是否啓動了gps s ervice? – Yashasvi

+0

我在按鈕的onClick方法中啓動它。 – jobin

+0

我的問題類似於http://stackoverflow.com/questions/25616956/which-activity-life-cycle-gets-called-when-app-redirect-to-settings – jobin

回答

0

不要的onResume檢查GPS狀態。 使用startActivityForResult打開設置。

並檢查onActivityResult

GPS狀態

要打開下面的代碼設置使用

Intent SettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
SettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivityForResult(SettingIntent,Utils.SETTING_INTENT_REQUESTCODE// any int code you can use); 

要處理結果,你需要重寫onActivityResult活動的方法

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == Utils.SETTING_INTENT_REQUESTCODE// this should be same int that you passed to start acitivity) { 
      LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      boolean isproviderenabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
      if(isproviderenabled==false){ 
       if(gps_intent!=null){ 
        Log.e("g","g"); 
        stopService(gps_intent); 
       } 
      } 
     } 
    } 

希望它會爲工作你

+0

我之前嘗試過使用onActivityResult,但在設置打開時調用它 – jobin

+0

您可能在android:launchMode =「singleTop」的Manifest中聲明瞭Activity。嘗試更改它與android:launchMode =「標準」,並檢查 –

+0

您從哪個活動啓動設置意圖 –