【发布时间】:2014-07-17 14:54:46
【问题描述】:
我想检测 GPS 的开关。
我的想法是使用 IntentService 和 Broadcast intent。 我的服务正在运行,但我的广播接收器不工作。
如果用户打开/关闭 GPS,我只想告诉我的 HomeActivity。有了这些信息,我将能够使用当前位置设置我的 GoogleMap。
谢谢!
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();
}
}
}
}
【问题讨论】:
-
你有什么问题?
-
它不起作用。我不知道为什么。我的服务已启动,但我的广播接收器似乎没有收到任何信息。当我启用 GPS 时,没有 Toast 出现。
-
您不需要服务来接收 GPS 状态变化的广播。您是否仅在您的应用运行时才需要它?
-
是的。我想在启用 GPS 时在地图上设置标记。当他被禁用时,标记会改变颜色。如果有一个更简单的解决方案,我很感兴趣,但我想知道如何使用 IntentServices 进行此操作
-
没有理由为此使用
IntentService。接收广播不需要做任何工作。如果您需要针对状态更改进行工作,可以在IntentService中完成。要接收状态更改通知,请在您的Activity中注册BroadcastReceiver,然后进行相应的工作。
标签: java android broadcastreceiver android-intentservice