2014-07-17 3 views
-1

GPS 스위치를 감지하고 싶습니다.GPS를 사용하는 경우 브로드 캐스트 서비스를 제공합니다.

제 아이디어는 IntentService와 Broadcast intent를 사용하는 것입니다. 서비스가 실행 중이지만 브로드 캐스트 수신기가 작동하지 않습니다.

사용자가 GPS를 켜고 끄는 경우 내 HomeActivity에하고 싶습니다. 이 정보를 통해 현재 위치로 Google지도를 설정할 수 있습니다.

Thx!

GpsService.java : 예를 들어

public class GpsService extends IntentService implements LocationListener { 

    private final static String TAG = "com.example.smartoo.gpsservice"; 

    public GpsService() { 
     super(TAG); 
     Log.e("TAG", "GPS service k"); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     if (LocationManager.GPS_PROVIDER.equals(provider)) { 
      Intent i = new Intent("android.location.PROVIDERS_CHANGED"); 
      i.putExtra("enabled", 1); 
      sendBroadcast(i); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     if (LocationManager.GPS_PROVIDER.equals(provider)) { 
      Intent i = new Intent("android.location.PROVIDERS_CHANGED"); 
      i.putExtra("disnabled", 1); 
      sendBroadcast(i); 
     } 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

    } 
} 

HomeActivity.java

public class HomeActivity extends ActionBarActivity implements LocationListener, 
                   GooglePlayServicesClient.ConnectionCallbacks, 
                   GooglePlayServicesClient.OnConnectionFailedListener { 

    //My attrs 
    private GoogleMap mMap = null; 
    Location currentLocation; 


    /* 
    * Initialize the Activity 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home_layout); 

     //Get the map from xml file 
     mMap = getMap(); 

     Intent i = new Intent(HomeActivity.this, GpsService.class); 
     startService(i); 
    } 

    /* 
    * Called when the Activity is restarted, even before it becomes visible. 
    */ 
    @Override 
    public void onStart() { 

     super.onStart(); 

     final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     //Check if GPS is enable on device. 
     if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      showGPSDisabledAlertToUser(); 
     } 
    } 


public class GpsLocationReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) { 
       int extra = intent.getIntExtra("enabled", -1); 
       //If GPS not enabled 
       if(extra == -1) { 
        extra = intent.getIntExtra("disabled", -1); 
        //If GPS not disabled 
        if (extra == -1) { 
         //Nothing 
        } 
        //If GPS disabled 
        else { 
         //Do something 
        } 
       } 
       //If GPS enabled 
       else { 
        Toast.makeText(HomeActivity.this, "GPS enabled", Toast.LENGTH_LONG).show(); 
        currentLocation = getLocation(); 
        setUpMapIfNeeded(); 
       } 
       Toast.makeText(HomeActivity.this, "in onREceive", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 
+1

귀하의 질문은 무엇입니까? – 323go

+0

작동하지 않습니다. 나는 이유를 모른다. 서비스가 시작되었지만 방송 수신자가 정보를받지 못하는 것으로 보입니다. No Toast는 GPS를 사용할 때 나타납니다. – Maxouille

+0

GPS 상태 변경의 브로드 캐스트를 수신하기 위해 서비스가 필요하지 않습니다. 앱이 실행 중일 때만 필요합니까? –

답변

1

먼저 매니페스트에 다음 권한을 추가 :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

를 그런 간단한 활동을 :

public class MainActivity extends Activity 
{ 
    private GpsStatusReceiver receiver = new GpsStatusReceiver(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     registerReceiver(receiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)); 
    } 

    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
     unregisterReceiver(receiver); 
    } 

    private class GpsStatusReceiver extends BroadcastReceiver 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      LocationManager lm = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE); 
      boolean isEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
      onGpsStatusChanged(isEnabled); 
     } 
    } 

    private void onGpsStatusChanged(boolean isEnabled) 
    { 
     // Do/start your work here. 
     Toast.makeText(this, "GPS enabled - " + isEnabled, 0).show(); 
    } 
} 
+1

당신의 코드 작업은 당신에게 대단히 감사한다. 이제 Google 재생 서비스 및 위치 업데이트 방법에 "onreceive"라는 다른 문제가 있습니다. p – Maxouille