Merge background task into develop #14

Merged
s416204 merged 17 commits from background-to-merge into develop 2018-10-12 00:12:34 +02:00
2 changed files with 48 additions and 6 deletions
Showing only changes of commit ae5c8d91e2 - Show all commits

View File

@ -5,6 +5,7 @@ import android.content.BroadcastReceiver;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build;
import android.support.design.widget.BottomNavigationView; import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -132,10 +133,17 @@ public class MainActivity extends AppCompatActivity {
if (isTutor) { if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startService(intent);
} else {
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(); 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.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location; import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
@ -104,10 +109,17 @@ public class BackgroundLocalizationService extends Service {
Log.e(TAG, "onCreate"); Log.e(TAG, "onCreate");
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
.setContentText("Content").build(); startMyOwnForeground();
else {
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build();
startForeground(1001, notification);
}
startForeground(1001, notification);
initializeLocationManager(); initializeLocationManager();
@ -142,7 +154,26 @@ public class BackgroundLocalizationService extends Service {
intent = new Intent(str_receiver); 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() { private void fn_getlocation() {
@ -220,6 +251,9 @@ public class BackgroundLocalizationService extends Service {
} }
private class TimerTaskToGetLocation extends TimerTask{ private class TimerTaskToGetLocation extends TimerTask{
@Override @Override
public void run() { public void run() {