- Forum-Beiträge: 39
07.09.2018, 08:32:38 via Website
07.09.2018 08:32:38 via Website
Hey,
ich hatte hier vor ein paar Wochen schon mal eine Frage gepostet, wie ich GPS updates im Hintergrund permanent erhalte. Mir wurde die Antwort gegeben einen Service mit einem Heartbeat zu verwenden. Danke für die Hilfe nochmal.
Jetzt habe ich das Problem, dass mein Service trotzdem ab und zu gekillt wird.
Ich habe jetzt zwei troubleshooting Ansätze:
Entweder die App crasht nach ein paar stunden und ich krieg nicht mit wieso. In diesem Kontext würde mich interessieren, ob ich die Logfiles auf meinem Gerät speichern kann und später auslesen? Ich habe nur dieses eine Gerät und kann es nicht die ganze Zeit am Rechner hängen lassen
Der zweite Ansatz ist, dass etwas mit meinem Heartbeat nicht stimmt. Hierfür poste ich hier mal meinen Code:
Die MainActivity:
> public class MainActivity extends AppCompatActivity {
//initializing heartbeat
static AlarmManager alarmManager;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//checking if permission to use gps location is given
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 10);
return;
}else{
Toast.makeText(MainActivity.this,"Permission to use GPS is given!", Toast.LENGTH_SHORT).show();
}
Log.e("Alarmmanager", "Starting Alarmmanager");
if (alarmManager == null && !isMyServiceRunning(BackroundService.class)) {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 900000, pendingIntent);
}
}
Der wichtigste Part ist der Unterste mit dem AlarmManager.
Dann die AlarmReceive klasse:
public class AlarmReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, BackroundService.class);
context.stopService(background);
context.startService(background);
}
}
Und zu guter Letzt der BackroundService:
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
//code for letting the service run even if app is not used
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_NONE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(notificationChannel);
// 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")
.build();
startForeground(9, notification);
}
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//when the location changed
//here is where the magic is happening
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(BackroundService.this, "gps is turned off!", Toast.LENGTH_SHORT).show();
}
};
requestingGpsUpdates();
return START_STICKY;
}
public void requestingGpsUpdates(){
powerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
//Handler to delay
new Handler().postDelayed(new Runnable() {
@SuppressLint("MissingPermission")
@Override
public void run() {
if (powerManager.isScreenOn()){
Log.d("Requesting:","GpsLocation");
//asking for a new update
if (!updatesRunning){
locationManager.requestLocationUpdates("gps", 500, 0, locationListener);
updatesRunning = true;
Log.e("Updates Requested", ""+updatesRunning);
}
}else{
//if screen is turned off updates will be removed
if (updatesRunning){
locationManager.removeUpdates(locationListener);
updatesRunning = false;
Log.e("Updates Requested", ""+updatesRunning);
}
}
//the method calling itself for a loop
if (!isDestroy){
requestingGpsUpdates();
}else {
Log.e("Service", "get's destroyed!");
}
}
},500);
}
Könntet ihr mir vielleicht dabei helfen? Ich habe schon viel gesucht, und keine Lösung bekommen.
— geändert am 07.09.2018, 08:35:22