- Forum-Beiträge: 39
21.09.2018, 09:26:32 via Website
21.09.2018 09:26:32 via Website
Hallihallo,
Ich habe, nachdem ich das Problem mit dem Hintergrund Service dank eurer Hilfe gelöst habe, ein neues Problem:
Meine Location Updates werden nur gemacht, wenn Android mit einem API Level => 26 installiert ist. Wenn der/das (?) API Level darunter liegt passiert nüscht...
Hier ist meine LocationUpdateService Klasse:
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Location Updates", "*************Started**************");
//Instantiating the device manager an listener
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//when the location changed
//here is where the magic happens...
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(LocationUpdateService.this, "gps is turned off!", Toast.LENGTH_SHORT).show();
}
};
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setSpeedAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(false);
criteria.setBearingRequired(false);
//API level 9 and up
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
locationManager.requestLocationUpdates(1000, 0, criteria, locationListener, null);
return START_STICKY;
}
und hier meine Backround Service Klasse:
public class BackroundService extends Service {
boolean isDestroy;
boolean updatesRunning;
//initializing the BR for checking if the screen is turned on or off
BroadcastReceiver broadcastReceiverScreen = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//if screen is turned on
if(Intent.ACTION_SCREEN_ON.equals(intent.getAction())){
//asking for a new update
if (!isMyServiceRunning(LocationUpdateService.class)) {
try {
startService(new Intent(BackroundService.this, LocationUpdateService.class));
Log.e("Updates Requested", "" + isMyServiceRunning(LocationUpdateService.class));
} catch (Exception e) {
Log.e("ScreenTurnedOn", "" + e);
}
}
}
//if screen is turned off
if(Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){
//if screen is turned off updates will be removed
if (isMyServiceRunning(LocationUpdateService.class)){
try {
stopService(new Intent(BackroundService.this, LocationUpdateService.class));
Log.e("Updates Requested", ""+isMyServiceRunning(LocationUpdateService.class));
}catch (Exception e){
Log.e("ScreenTurnedOff", ""+e);
}
}
}
}
};
//Method to make the service work
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//method that get's started on the start of the service
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
Log.e("New Service","################STARTED#################");
isDestroy = false;
//Intent filter for BR checking if screen is turned on
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
//Broadcast receiver for checking if Screen is turned on
registerReceiver(broadcastReceiverScreen, screenStateFilter);
//code for letting the service run even if app is not used
try{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// For foreground service
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Creating channel for notification
String id = BackroundService.class.getSimpleName();
String name = BackroundService.class.getSimpleName();
NotificationChannel notificationChannel = new NotificationChannel(id,
name, NotificationManager.IMPORTANCE_LOW);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(notificationChannel);
notificationChannel.setSound(null, null);
// Foreground notification
Notification notification = new Notification.Builder(this, id)
.setContentTitle(getText(R.string.app_name))
.setContentText("KSL is protecting you!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setTicker("Ticker text")
.setChannelId(id)
.build();
startForeground(9, notification);
}else{
String id = BackroundService.class.getSimpleName();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getText(R.string.app_name))
.setContentText("KSL is protecting you!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setTicker("Ticker text");
Notification notification = builder.build();
startForeground(1, notification);
}
}catch (Exception e){
Log.e("Notification", ""+e);
}
try{
MainActivity.imageViewShield.setImageResource(R.drawable.shield_green);
}catch (Exception e){
Log.e("Setting green Shield", ""+e);
}
return START_STICKY;
}
@Override
public void onDestroy() {
MainActivity.imageViewShield.setImageResource(R.drawable.shield_red);
super.onDestroy();
try {
unregisterReceiver(broadcastReceiverScreen);
}catch (Exception e){
Log.e("unregisterReceiver", ""+e);
}
try {
stopService(new Intent(BackroundService.this, LocationUpdateService.class));
isDestroy = true;
}catch (Exception e){
Log.e("RemovingUpdatesDestroy", ""+e);
}
try{
MainActivity.imageViewShield.setImageResource(R.drawable.shield_red);
}catch (Exception e){
Log.e("Setting Red Shield", ""+e);
}
}
Wenn ich meine Logfiles auslese, kann ich sehen, dass der LocationUpdateService gestartet wird, aber keine Gps updates empfangen.
Hab ihr ne idee, woran das liegen könnte?
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.