如果启用了GPS,请使用广播服务

时间:2014-07-17 14:54:47

标签: java android broadcastreceiver android-intentservice

我想检测GPS的开关。

我的想法是使用IntentService和Broadcast意图。 我的服务正在运行,但我的广播接收器不起作用。

如果用户打开/关闭GPS,我只想对我的HomeActivity说。有了这些信息,我将能够使用当前位置设置我的GoogleMap。

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 个答案:

答案 0 :(得分: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();
    }
}