从后台捕获位置并将其上传到云端或保存到 android 中的本地存储
Capture location from background and upload that to cloud or save to local storage in android
我想创建一个 android 应用程序,即使应用程序不是 运行,它也可以捕获设备位置。我曾尝试制作一个 returns 数据从服务到主线程的应用程序。该应用程序在 运行 时运行良好,但如果该应用程序关闭,则它不会捕获任何位置。
我已附加我的服务 class。现在它只是捕获位置并更新 UI。即使应用程序处于 暂停,它仍 正在工作。但是当应用程序 被销毁 .
时,它 不工作
public class DeviceLocationService extends Service {
private LocationListener listener;
private LocationManager locationManager;
private final String TAG = this.getClass().getName();
private int count = 0;
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent(Config.LOCATION_INTENT_FILTER);
HashMap<Object,Object> hashMap = new HashMap<>();
Log.d(TAG, "onAccuracyChanged: "+location.getAccuracy());
hashMap.put("latitude",location.getLatitude());
hashMap.put("longitude",location.getLongitude());
hashMap.put("accuracy",location.getAccuracy());
hashMap.put("provider",location.getProvider());
JSONObject jo = new JSONObject(hashMap);
i.putExtra("location_data",jo.toString());
Log.d(TAG, "onLocationChanged: "+jo.toString());
sendBroadcast(i);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged: provider "+provider);
Log.d(TAG, "onStatusChanged: status "+status);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: "+provider);
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,listener);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
要运行您的后台服务需要使用前台服务。
在您的服务中添加以下代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
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 notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
并使用以下代码启动服务:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this,DeviceLocationService.class));
} else
startService(new Intent(this,DeviceLocationService.class));
我想创建一个 android 应用程序,即使应用程序不是 运行,它也可以捕获设备位置。我曾尝试制作一个 returns 数据从服务到主线程的应用程序。该应用程序在 运行 时运行良好,但如果该应用程序关闭,则它不会捕获任何位置。
我已附加我的服务 class。现在它只是捕获位置并更新 UI。即使应用程序处于 暂停,它仍 正在工作。但是当应用程序 被销毁 .
时,它 不工作public class DeviceLocationService extends Service {
private LocationListener listener;
private LocationManager locationManager;
private final String TAG = this.getClass().getName();
private int count = 0;
@SuppressLint("MissingPermission")
@Override
public void onCreate() {
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent(Config.LOCATION_INTENT_FILTER);
HashMap<Object,Object> hashMap = new HashMap<>();
Log.d(TAG, "onAccuracyChanged: "+location.getAccuracy());
hashMap.put("latitude",location.getLatitude());
hashMap.put("longitude",location.getLongitude());
hashMap.put("accuracy",location.getAccuracy());
hashMap.put("provider",location.getProvider());
JSONObject jo = new JSONObject(hashMap);
i.putExtra("location_data",jo.toString());
Log.d(TAG, "onLocationChanged: "+jo.toString());
sendBroadcast(i);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged: provider "+provider);
Log.d(TAG, "onStatusChanged: status "+status);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: "+provider);
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,listener);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
要运行您的后台服务需要使用前台服务。
在您的服务中添加以下代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
return START_STICKY;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
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 notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
并使用以下代码启动服务:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this,DeviceLocationService.class));
} else
startService(new Intent(this,DeviceLocationService.class));