Hier der Service:
public class EndlessService extends Service
{
private boolean isServiceStarted = false;
public IBinder onBind(Intent intent)
{
return null;
}
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
if (this.isServiceStarted)
{
Log.i(this.getClass().getSimpleName(), "App Service bereits gestartet!");
}
else
{
this.startGPSTracking();
Log.i(this.getClass().getSimpleName(), "App Service ist gestartet!");
this.isServiceStarted = true;
}
// by returning this we make sure the service is restarted if the system kills the service
return START_STICKY;
}
public void onCreate(){
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = createNotification();
startForeground(10, notification);
}
else
{
Intent endlessServiceIntent = new Intent(getApplicationContext(), EndlessService.class);
if(endlessServiceIntent != null)
{
Notification notification = createNotificationSmaler26Mode();
startForeground(10, notification);
}
}
}
/**
*
*/
public void onDestroy()
{
super.onDestroy();
Log.i(this.getClass().getSimpleName(), "App Service wurde beendet - Destroy".toUpperCase());
}
private Notification createNotification()
{
String notificationChannelId = "ENDLESS SERVICE CHANNEL";
Notification newMessageNotification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
NotificationChannel serviceChannel = new NotificationChannel(
notificationChannelId,
"Notification",
NotificationManager.IMPORTANCE_HIGH
);
serviceChannel.setDescription("Notification");
serviceChannel.enableLights(false);
serviceChannel.setLightColor(Color.RED);
serviceChannel.enableVibration(false);
long a[] = {100, 200, 300, 400, 500, 400, 300, 200, 400};
serviceChannel.setVibrationPattern(a);
notificationManager.createNotificationChannel(serviceChannel);
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
newMessageNotification = new Notification.Builder(this, notificationChannelId)
.setContentTitle("Notifications")
.setContentText("Notifications")
.setContentIntent(pendingIntent)
.setTicker("")
.setPriority(Notification.PRIORITY_HIGH)
.build();
}
return newMessageNotification;
}
/**
*
* @return
*/
private Notification createNotificationSmaler26Mode()
{
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification noti = new Notification.Builder(getApplicationContext())
.setContentTitle("Notifications")
.setContentText("Notifications")
.setContentIntent(pendingIntent)
.build();
return noti;
}
/**
*
*/
public void startGPSTracking()
{
APPLocationListener appLocationListener = new APPLocationListener(getBaseContext());
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(EndlessService.this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25000, 80, appLocationListener);
Log.i(this.getClass().getSimpleName(), "GPS Tracking hat begonnen!");
}
}
}
}
Unter Android 9 ging alles wunderbar!
Danke!
— geändert am 08.04.2020, 20:07:41
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.