Add support for android O

This commit is contained in:
Mieszko Wrzeszczyński 2018-10-09 21:33:44 +02:00
parent 6d6db2efe0
commit ae5c8d91e2
2 changed files with 48 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
@ -133,7 +134,14 @@ public class MainActivity extends AppCompatActivity {
if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();

View File

@ -3,15 +3,20 @@ package com.uam.wmi.findmytutor.service;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@ -104,11 +109,18 @@ public class BackgroundLocalizationService extends Service {
Log.e(TAG, "onCreate");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else {
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build();
startForeground(1001, notification);
}
initializeLocationManager();
try {
@ -142,7 +154,26 @@ public class BackgroundLocalizationService extends Service {
intent = new Intent(str_receiver);
}
@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)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
private void fn_getlocation() {
@ -220,6 +251,9 @@ public class BackgroundLocalizationService extends Service {
}
private class TimerTaskToGetLocation extends TimerTask{
@Override
public void run() {