我正在Android OS上开发一款应用.我不知道如何检查是否启用了位置服务.
我需要一个方法,如果它们被启用则返回"true",否则返回"false"(所以在最后一种情况下我可以显示一个对话框来启用它们).
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
} else {
Toast.makeText(context, "not in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
@Override
public void onLocationChanged(Location arg0) {
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我需要在打开/关闭gps时触发广播接收器.调查此事并建议在app中实施最好的一个.
我在MainActivity中有两个文件MainActivity.java和HomeFragment.java从HomeFragment调用一个函数,该函数要求用户打开手机上的位置服务.问题是即使用户已经打开了位置功能,仍然会调用该功能.有没有办法我可以这样做只有当位置功能关闭时才启动HomeFragment中的功能.
HomeFragment.java
public static void displayPromptForEnablingGPS(
final Activity activity)
{
final AlertDialog.Builder builder =
new AlertDialog.Builder(activity);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "Enable either GPS or any other location"
+ " service to find current location. Click OK to go to"
+ " location services settings to let you do so.";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
activity.startActivity(new Intent(action));
d.dismiss();
}
});
builder.create().show();
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
public void showMainView() {
HomeFragment.displayPromptForEnablingGPS(this);
} …
Run Code Online (Sandbox Code Playgroud) 我正在构建一个应用程序,我必须不断保存用户的位置,然后将其发送到服务器.为此,我在服务中使用FusedLocationApis.我第一次要求用户打开位置.它工作得很好.
现在,如果用户无意中关闭了该位置该怎么办.我该如何通知他?开发人员必须处理这个问题,还是只留给用户?
我想给他一个android通知.为此,我需要不断检查我们在服务中启用的位置.我有一个工作代码(函数checkHighAccuracyLocationMode).但是我应该在哪里检查(checkHighAccuracyLocationMode)?每次在此服务中启动哪个功能?或者,无论如何,当用户关闭他的位置时,我可以获得一个功能?
这是我的服务:
public class locationUpdatesService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Location mLastLocation;
private Location mCurrentLocation;
private String mLastUpdateTime;
private dataBaseHandler db_helper;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
initialise();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
db_helper = new dataBaseHandler(getApplicationContext(),dataBaseHandler.DATABASE_NAME,null,1);
}
@Override
public void onDestroy() {
stopLocationUpdates();
singletGoogleClientApi.setinstancenull();
singletLocationRequest.setinstancenull();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void initialise(){
Log.e("ghgdhu","qaws");
if (singletGoogleClientApi.getinstance().getGoogleApiCLient() == null) …
Run Code Online (Sandbox Code Playgroud)