【发布时间】:2020-10-05 08:15:06
【问题描述】:
我有定位服务:
public class LocationService extends Service implements Serializable {
public static final String TAG = "LocationService";
private LocationListener locationListener;
private LocationManager locationManager;
public static boolean locationUpdateSent = false;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
startMyOwnForeground();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onLocationChanged(Location location) {
buildLocationEvent(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "LocationChannel";
String channelName = "Location Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.GRAY);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification locNotification = notificationBuilder.setOngoing(true)
.setContentTitle("Location is running in background")
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(android.R.drawable.ic_menu_mylocation)
.build();
startForeground(1, locNotification);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
locationManager.removeUpdates(locationListener);
}
stopSelf();
stopForeground(true);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void buildLocationEvent(Location location){
//Do Something
}
}
这适用于 Pixel 3A 和第二个 Pixel,但不适用于 Galaxy S10、A5 和 Pixel XL。有没有人有任何可以阻止这种情况发生的限制的经验?我知道这没什么可做的,但希望如果您在看到这种行为之前能够为我指明正确的方向。
【问题讨论】:
标签: java android android-service android-location