Android 如何解决GPS在后台自动关闭的问题?
我正在尝试用android中的GPS获取坐标。我成功地在前景中获得了位置,但每当我将应用程序放在背景中时,GPS标志就会消失,并且在背景中停止定位点。 我使用了不同版本的android,有些是定位点(低于8.0),有些不是(Pie) 如何在Pie的后台运行GPS。我尝试了不同的SO答案,但没有得到解决方案 我正在使用该服务在后台运行应用程序,并使用广播接收器获取坐标。但我并没有得到派版本的分数 这是我的MainActivity.java 这是我的服务课 这是舱单Android 如何解决GPS在后台自动关闭的问题?,android,gps,location,Android,Gps,Location,我正在尝试用android中的GPS获取坐标。我成功地在前景中获得了位置,但每当我将应用程序放在背景中时,GPS标志就会消失,并且在背景中停止定位点。 我使用了不同版本的android,有些是定位点(低于8.0),有些不是(Pie) 如何在Pie的后台运行GPS。我尝试了不同的SO答案,但没有得到解决方案 我正在使用该服务在后台运行应用程序,并使用广播接收器获取坐标。但我并没有得到派版本的分数 这是我的MainActivity.java 这是我的服务课 这是舱单 通过使用startForegro
通过使用
startForegroundService()
而不是startService()
我得到了Oreo(8.0)及以上版本的解决方案(在后台获取GPS位置)
对于实现,我们必须在清单中添加前台权限和startForeground()服务类中的代码>方法,如下所示:
这是我更新的activity.java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (!runtime_permissions()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Intent serviceIntent = new Intent(context, YourService.class);
ContextCompat.startForegroundService(context, serviceIntent );
}
else
{
context.startService(new Intent(context, YourService.class));
}
}
}
更新了我的服务类别
@SuppressLint("InvalidWakeLockTag")
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
...
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLightColor(Color.BLUE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(chan);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
更新的清单权限
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
。。。
从Andriod 8及更高版本开始,后台服务的位置更新在一小时内只收到几次。尝试使用前台服务。@ShubhamPanchal-您能参考或回答前台服务的代码吗?这将是极大的帮助。我正在尝试,但每次都失败了。你可以参考do官方文档。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (!runtime_permissions()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
Intent serviceIntent = new Intent(context, YourService.class);
ContextCompat.startForegroundService(context, serviceIntent );
}
else
{
context.startService(new Intent(context, YourService.class));
}
}
}
@SuppressLint("InvalidWakeLockTag")
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
...
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLightColor(Color.BLUE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(chan);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>