我读过Google doc这post特别是有趣@ bendaf的回答,有关管理位置设置,一切正常。
反正一个问题仍然存在,如果用户第一次决定不使用它的位置,但后来决定将其激活,应用程序不受此动作触发,所以我不知道,我可以要求定期更新。
我错过了什么吗?
你可以提供一个AlertDialog每次用户启动一个应用程序,使用户可以启用位置时间。但是,这将是一个有点恼人,因为每次你有时间拒绝同样的事情。
另外,您也可以使用偏好,以便用户可以启用/禁用位置
还有另一种更好的方式来做到这一点。使用LocationListener的。这是要达到的唯一目的。
public class XYZ Activity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
我认为这是你正在寻找正确的事情。这是事件的方式。编码愉快:)
@fralbo虽然这个线程是一点点的时候,我只是想为你提供一个解决方案,因为我最近有做类似的事情我自己。
我会建议实施BroadcastReceiver
到您的应用程序,与意图过滤PROVIDERS_CHANGED
..
这将触发每次定位/ GPS提供商状态的变化 - 你可以使用一个if
语句内BroadcastReceiver
的onReceive
方法,以确定您需要的条件是否被满足,并进行相应处理。例如,在onReceive
方法内在BroadcastReceiver
,你可以确定PROVIDERS_CHANGED
事件是否已变得可用的GPS(到什么程度) - 如果它现在满足你的应用程序的需求,然后你可以打电话给你的应用程序,它负责启动中的任何一种方法需要调用GPS引擎,等等。
这是一个什么样的代码可能看起来像一个例子:
public class LocationReceiver extends BroadcastReceiver {
private final static String TAG = "[ LocationReceiver ]:";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "PROVIDERS_CHANGED has been detected - Firing onReceive");
// Retrieve the LocationManager
LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Provide boolean references for Location availability types
boolean isGpsEnabled;
boolean isNetworkEnabled;
// Provide values to retrieve the Location availability
isGpsEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled =
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.i(TAG, "Detected - GPS: "+ isGpsEnabled + "NET: "+ isNetworkEnabled);
// If (for example), the GPS is ENABLED, start one of your Activities, etc.
if (isGpsEnabled) {
Intent startYourActivity =
new Intent(context.getApplicationContext(), YourActivity.class);
context.startActivity(startYourActivity);
}
}
}
我希望这有帮助!迟到总比不到好 :) 编码愉快!