问题描述
我正在为驱动程序开发一个 android 应用程序,我正在通过融合位置更新获取设备位置更新。
我也在使用 JobService 和其他可以帮助我获取位置更新的方法。我的应用程序在后台完美运行。同样,当我关闭我的应用程序时,我会不断获取我的位置更新,但是当我的 android 屏幕关闭时,位置更新就会停止,我无法接收我的位置。我也在使用广播接收器在后台运行我的服务,但融合的位置服务不起作用。我使用的是 android 10。当我解锁我的 android 设备时,位置不会自动启动。
但是当我在 android 9 中使用此代码时,它就可以工作了。如果屏幕关闭则无法工作,但当我解锁屏幕时,它会再次自动在 android 9 中工作。
我也在使用 GPS 定位。但 GPS 也有同样的问题。
这是我的 Manifest.xml 权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
这是我的广播接收器代码。
@Override
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_ON.equals(action)) {
MediaPlayer MP = MediaPlayer.create(context,Settings.System.DEFAULT_ALARM_ALERT_URI);
MP.start();
startLocationService(context);
} else if(Intent.ACTION_SCREEN_OFF.equals(action)) {
MediaPlayer MP = MediaPlayer.create(context,Settings.System.DEFAULT_ALARM_ALERT_URI);
MP.start();
startLocationService(context);
}
}
private void startLocationService(Context context){
Intent intent = new Intent(context,LocationService.class);
intent.setAction(Constants.ACTION_START_SERVICE_LOCATION);
context.startService(intent);
context.getApplicationContext().startService(intent);
}
这是我的工作服务代码。
private boolean JobCancle = false;
@Override
public boolean onStartJob(JobParameters jobParameters) {
dobackgroundWork(jobParameters);
return true;
}
private void dobackgroundWork(final JobParameters jobParameters)
{
boolean foregroud = false;
final DbOnline dbOnline = new DbOnline(this);
try {
foregroud = new CheckAppRunning().execute(this).get();
} catch (ExecutionException e) {
e.printstacktrace();
} catch (InterruptedException e) {
e.printstacktrace();
}
Timer timer = new Timer();
final boolean finalForegroud = foregroud;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if(finalForegroud == false)
{
Log.d("Job","This Jobs False");
Intent intent = new Intent(getApplicationContext(),LocationService.class);
intent.setAction(Constants.ACTION_START_SERVICE_LOCATION);
startService(intent);
}
if(finalForegroud == true)
{
Log.d("Job","This Jobs True");
Intent intent = new Intent(getApplicationContext(),LocationService.class);
intent.setAction(Constants.ACTION_START_SERVICE_LOCATION);
startService(intent);
}
};
};
timer.schedule(timerTask,1000);
jobFinished(jobParameters,false);
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
JobCancle = true;
return true;
}
这是我的定位服务代码。
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult != null && locationResult.getLastLocation() != null) {
double latitute = locationResult.getLastLocation().getLatitude();
double longitude = locationResult.getLastLocation().getLongitude();
Sessions sessions = new Sessions(LocationService.this);
DbOnline dbOnline = new DbOnline(LocationService.this);
dbOnline.GetSession();
//Log.i("Location Update",latitute + " : " + longitude);
dbOnline.SendLocation(latitute,longitude);
//Toast.makeText(LocationService.this,"Latitude "+latitute+",Longitude "+longitude,Toast.LENGTH_SHORT).show();
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not Implement Yet");
}
private void startLocationService() {
String ChannelID = "location_notification_channel";
notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),resultIntent,PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(),ChannelID
);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("ETill Driver");
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 service is used for Driver Pro Application");
notificationmanager.createNotificationChannel(notificationChannel);
}
}
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(6000);
locationRequest.setFastestInterval(4000);
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) {
Toast.makeText(this,"Please Give Us Permission",Toast.LENGTH_SHORT).show();
return;
}
LocationServices.getFusedLocationProviderClient(this)
.requestLocationUpdates(locationRequest,locationCallback,Looper.getMainLooper());
startForeground(Constants.LOCATION_SERVICE_ID,builder.build());
}
private void stopLocationService(){
if (ActivityCompat.checkSelfPermission(this,Toast.LENGTH_SHORT).show();
return;
}
LocationServices.getFusedLocationProviderClient(this)
.removeLocationUpdates(locationCallback);
//stopForeground(true);
//stopSelf();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
super.onStartCommand(intent,flags,startId);
if(intent != null)
{
String action = intent.getAction();
if(action != null)
{
if(action.equals(Constants.ACTION_START_SERVICE_LOCATION))
{
startLocationService();
}
else if(action.equals(Constants.ACTION_STOP_SERVICE_LOCATION))
{
stopLocationService();
}
}
}
return START_STICKY;
}
这是我的 AppCheckinginBackground.java 代码
@Override
protected Boolean doInBackground(Context... params){
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context){
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
{
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName))
{
return true;
}
}
return false;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)