2012-09-08 37 views
0

我應該怎麼做,以關閉GPS,如果位置不是在第一次發現instance..without位置更新被稱爲全球定位系統劑量不會關閉

package mobile.sales; 

import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.util.Log; 

public class test extends Activity 
{ 
    DataSource dataSource; 
    Double lat=0.0,lng=0.0; 
    String GPS_FILTER = ""; 
    Thread triggerService; 
    LocationListener locationListener; 
    LocationManager lm; 
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters 
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 29*1000*60; // in Milliseconds 
    protected LocationManager locationManager; 
    boolean isRunning = true; 
    NotificationManager notificationManager; 
    PendingIntent contentIntent; 
    Notification notification; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     turnGPSOn(); 
     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locationListener = new MyLocationListener(); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener); 
    } 

    @Override 
    public void onDestroy() 
    { 
     // TODO Auto-generated method stub 
     locationManager.removeUpdates(locationListener); 
     locationManager= null; 
     turnGPSOnOff(); 
     super.onDestroy(); 
     // removeGpsListener(); 
    } 

    private class MyLocationListener implements LocationListener 
    { 
     public void onLocationChanged(Location location) 
     { 
      lat=location.getLatitude(); 
      lng=location.getLongitude(); 
      turnGPSOnOff(); 
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
      String currentDateandTime = sdf.format(new Date());  
      SimpleDateFormat sdft = new SimpleDateFormat("hh:mm:ss"); 
      String time=sdft.format(new Date()); 
      dataSource.insertIntoLocationDetails(""+lat,""+lng,currentDateandTime,""+time); 
      Log.d("service","location Inserted"); 
     } 

     public void onStatusChanged(String s, int i, Bundle b) { 

     } 

     public void onProviderDisabled(String s) { 
     } 

     public void onProviderEnabled(String s) { 
     } 
    } 
    private void turnGPSOnOff(){ 
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", false); 
     sendBroadcast(intent); 
    } 
    private void turnGPSOn(){ 
      String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
      if(!provider.contains("gps")){ 
       final Intent poke = new Intent(); 
       poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider"); 
       poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
       poke.setData(Uri.parse("3")); 
       sendBroadcast(poke); 
      } 
    } 
} 
+0

任何人都可以請指導我我做錯了 – Sumit

回答

0

您呼叫的功能,當你正在嘗試DESTROY活動本身。首先使用GPSStatusListener

使用此代碼中找到GPS的修復

Check Gps Status

呼叫在GPSStatusListener

+0

沒有讓你我該怎麼辦? – Sumit

+0

如果在第一個實例沒有找到位置,我應該怎麼做才能關閉GPS?沒有位置更新被稱爲 – Sumit

+0

@Sumit首先使用GPSStatusListener來查找您的應用是否獲得了GPS修復,如果它在幾秒鐘後沒有獲得GPS修復在GPSStatusListener中調用你的函數。請在我提到的例子中仔細閱讀答案 – user996428

0

新的處理程序()你GpsTurnOff狀態。postDelayed(新的Runnable(){@Override public void run() turnGPSOnOff(); } },2000 * 60); 這將關閉我的GPS在一定的時間

相關問題