【发布时间】:2020-09-04 11:02:35
【问题描述】:
我正在使用的 android 服务遇到问题。该服务即将每 5 分钟获取一次用户的位置。一切似乎都很顺利,除非我杀死该应用程序,该服务不会做任何事情。在代码中,我认为您将每 60 秒检查一次,但这将更改为 5 分钟。
最低目标版本:API 23
目标版本:API 29
Android 清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.venomapps.meets">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:name=".Utils.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.RegisterActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.MainActivity" />
<activity android:name=".Activities.LoginActivity">
</activity>
<service android:name=".Services.LocationService"
android:enabled="true"
android:exported="true"/>
</application>
执行服务的代码:
AlarmManager am=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), LocationService.class); intent.setAction(Constants.ACTION_START_LOCATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), Constants.LOCATION_SERVICE_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 60000, pendingIntent);
服务:
public class LocationService extends Service {
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult != null && locationResult.getLastLocation() != null) {
double latitude = locationResult.getLastLocation().getLatitude();
double longitude = locationResult.getLastLocation().getLongitude();
Log.d("LOCATION_UPDATE", latitude + ", " + longitude);
stopLocationService();
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public void startLocationService() {
String channelId = "location_notification_channel";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(),
channelId
);
builder.setSmallIcon(R.drawable.ic_app);
builder.setContentTitle("Location Service");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentText("Running");
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(false);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (notificationManager != null && notificationManager.getNotificationChannel(channelId) == null) {
NotificationChannel notificationChannel = new NotificationChannel(
channelId, "Location Service", NotificationManager.IMPORTANCE_HIGH
);
notificationChannel.setDescription("This channel is used by location service");
notificationManager.createNotificationChannel(notificationChannel);
}
}
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(8000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.getFusedLocationProviderClient(this)
.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
startForeground(Constants.LOCATION_SERVICE_ID, builder.build());
}
private void stopLocationService(){
LocationServices.getFusedLocationProviderClient(this)
.removeLocationUpdates(locationCallback);
stopForeground(true);
stopSelf();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
String action = intent.getAction();
if(action != null){
if(action.equals(Constants.ACTION_START_LOCATION_SERVICE)){
startLocationService();
}
}
}
return super.onStartCommand(intent, flags, startId);
}
}
【问题讨论】:
-
“杀死应用程序”是什么意思?
-
不要将其留在后台。滑动它并确定关闭它。
-
添加一些调试日志。当您杀死应用程序时,
Service是否被杀死?Service会重新启动吗? -
我不明白你的代码。您触发
Service(即:每次警报触发时调用onStartCommand()。然后调用startLocationService(),它每次都会重新设置位置监控。这不是必需的。您只需执行一次。请找到执行此操作的操作方法或教程。
标签: java android service background android-service