2012-09-08 2 views
0

어떤 장소에 위치 업데이트가 당신은 기능 때를 호출GPS를 해제하지 복용량

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

를 호출되고 instance..without 1에서 발견되지 않으면 나는 GPS를 해제해야 노력하고 있습니다 DESTROY 활동 자체. 먼저

Check Gps Status

GPSStatusListener

+0

내가 어떻게해야합니까? – Sumit

+0

위치가 첫 번째 인스턴스에서 발견되지 않으면 GPS를 끄려면 어떻게해야합니까? – Sumit

+0

이라는 위치 업데이트가 없으면 @Sumit GPS가 수신되지 않았는지 확인하기 위해 GPSStatusListener를 먼저 사용하십시오. 몇 초 후에 GPS를 수정하지 않으면 GPSStatusListener 내에서 함수를 호출하십시오. 친절하게도 언급 한 예에서 신중하게 대답을 읽으십시오. – user996428

0

새로운 핸들러()에서 GpsTurnOff 상태를 전화 GPSStatusListener

사용이 코드를 사용하여 GPS의 수정을 찾을 수 있습니다. postDelayed (새의 Runnable() { @Override 을 public void run() { turnGPSOnOff(); } }, 2000 * 60); 특정 시간이 지나면 내 GPS가 꺼집니다

관련 문제