在支持Android Cupcake(1.5)的设备上,如何检查并激活GPS?
我在Android 2.3.7上,我正在尝试编写一个BroadcastReceiver,当用户打开/关闭GPS时(例如:通过点击选项中的"使用GPS卫星")将调用它.
到目前为止我所做的是:
1)创建了以下broadcastReceiver:
public class GPSStatusBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "Broadcast received!", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
2)将其添加到标签内的android清单中:
<receiver android:name=".GPSStatusBroadcastReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
现在问题在于它似乎不可靠.当我打开GPS/ff时onReceive()方法有时只运行(我会说1次超时3),其他时候它不会被调用.
我想知道是否有可靠的方法来检测GPS何时关闭/打开.
如果没有,那么至少在没有应用程序使用GPS anymoer时通知会很高兴(例如:当gps图标从状态栏消失时,表示没有人正在主动定位).
编辑:澄清:我的应用程序运行时我不想这样做.我只是在我的应用程序启动时打开/关闭gps,做它的事情然后终止,我不想订阅LocationsUpdates ...
在Android O(8.0.0+)(API 26+)中,如何在应用程序处于后台或终止时获取位置服务更新.在"Strava"Android应用程序(在Play商店中可用),当应用程序在后台或终止时,位置服务可以正常运行.我需要在我的应用程序中构建相同的功能.每当我开始使用服务
startService(new Intent(this, GpsServices.class));
Run Code Online (Sandbox Code Playgroud)
然后当应用程序处于空闲模式(应用程序在后台或终止)时,10秒后调用onDestroy方法.以下是我的代码.
public class GpsServices extends Service implements LocationListener, Listener {
@Override
public void onCreate() {
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mLocationManager != null) {
mLocationManager.addGpsStatusListener(this);
}
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
500,
0,
this);
}
}
public void onLocationChanged(Location location) {
System.out.println("location = [" + location + "]");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// If we …Run Code Online (Sandbox Code Playgroud) android background geolocation android-service android-8.0-oreo
我正在创建一个使用GPS的应用程序。这是onCreate()我第一次检查GPS是否已启用。如果未启用,那么我会将用户引导至设置菜单以设置GPS为启用状态。
启用GPS后,我便开始执行我的任务。但是,如果用户之间停止了通知管理器上的GPS滚动,那我怎么知道呢?
因为在未启用GPS的情况下,我的方法仅执行一次。此后,如果再次禁用它,则无法执行。
I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.
这是我的代码。
class LocationFinderService extends Service{
boolean gps_enabled;
LocationManager locationManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public boolean isGpsEnabled()
{
gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); …Run Code Online (Sandbox Code Playgroud) 我使用gms.location.LocationListener(Google Play服务)来获取用户的位置.它工作正常但我想检测用户何时禁用或启用他/她的GPS.如图所示.
当我打开/关闭位置时,不会调用任何方法.当我切换应用并返回我的应用时,会调用以下方法:
但是我需要在用户打开/关闭位置时调用该事件,而我列出的事件不会被调用.
注意:使用Android API,我能够使用onProviderEnabled和onProviderDisabled获取这些事件,我的问题是,在使用GoogleApiClient后,这些事件不再被调用.见图2和3.
之后onProviderEnabled和onProviderDisabled没有得到很好的调用,这是我想解决的另一个问题.
我的代码:
package facilito.codigo.app.dflores.com.myapplicationcf;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.location.Location;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationListener;//android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment; …Run Code Online (Sandbox Code Playgroud)