This commit is contained in:
Philipp Wo 2019-05-10 18:50:32 +02:00
parent 71057c27bd
commit e845195e9b
9 changed files with 193 additions and 1 deletions

View File

@ -4,9 +4,13 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:allowBackup="true"
android:name=".AirKoalityApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
@ -20,6 +24,7 @@
</activity>
<service android:name=".service.LocationService"/>
<activity android:name=".ui.activity.SplashActivity"
android:noHistory="true"

View File

@ -0,0 +1,32 @@
package at.fhj.airkoality;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class AirKoalityApplication extends Application {
public static final String CHANNEL_ID = "location_service_channel";
@Override
public void onCreate() {
super.onCreate();
//notification channel
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
NotificationChannel locationServiceChannel = new NotificationChannel(
CHANNEL_ID,
"Location Service Channel",
NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(locationServiceChannel);
}
}
}

View File

@ -0,0 +1,71 @@
package at.fhj.airkoality.service;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import at.fhj.airkoality.AirKoalityApplication;
import at.fhj.airkoality.R;
import at.fhj.airkoality.ui.activity.MainActivity;
public class LocationService extends Service {
private String TAG = "LocationService";
private Notification notification;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started");
startForeground(1337,notification);
return START_STICKY;
}
@Override
public void onCreate(){
Log.d(TAG, "Service created");
super.onCreate();
setupNotification();
}
@Override
public void onDestroy(){
Log.d(TAG, "Service destroyed");
super.onDestroy();
}
private void setupNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification = new NotificationCompat.Builder(this, AirKoalityApplication.CHANNEL_ID)
.setContentText("Updating location")
.setContentTitle("AirKoality-lab Location Service")
.setSmallIcon(R.drawable.ic_phone_android_black_24dp)
.setContentIntent(pendingIntent).build();
}
}

View File

@ -11,6 +11,8 @@ import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -26,6 +28,7 @@ import at.fhj.airkoality.db.room.AirKoalityDB;
import at.fhj.airkoality.model.Location;
import at.fhj.airkoality.network.HttpsGetTask;
import at.fhj.airkoality.network.RequestCallback;
import at.fhj.airkoality.service.LocationService;
import at.fhj.airkoality.ui.fragment.LocationListFragment;
import at.fhj.airkoality.ui.fragment.MapFragment;
@ -155,6 +158,46 @@ public class MainActivity extends AppCompatActivity implements RequestCallback {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menue, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_service_on:
startLocationService();
return true;
case R.id.action_service_off:
stopLocationService();
return true;
default:
return false;
}
}
private void startLocationService(){
Intent intent = new Intent(this, LocationService.class);
startService(intent);
}
private void stopLocationService(){
Intent intent = new Intent(this, LocationService.class);
stopService(intent);
}
private void updateFragments() {
runOnUiThread(() -> {
locationListFragment.refresh();

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#6C261F"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94V1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11H1v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94V23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94H23v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#1FFD45"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FD1400"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M2,16h15v3L2,19zM20.5,16L22,16v3h-1.5zM18,16h1.5v3L18,19zM18.85,7.73c0.62,-0.61 1,-1.45 1,-2.38C19.85,3.5 18.35,2 16.5,2v1.5c1.02,0 1.85,0.83 1.85,1.85S17.52,7.2 16.5,7.2v1.5c2.24,0 4,1.83 4,4.07L20.5,15L22,15v-2.24c0,-2.22 -1.28,-4.14 -3.15,-5.03zM16.03,10.2L14.5,10.2c-1.02,0 -1.85,-0.98 -1.85,-2s0.83,-1.75 1.85,-1.75v-1.5c-1.85,0 -3.35,1.5 -3.35,3.35s1.5,3.35 3.35,3.35h1.53c1.05,0 1.97,0.74 1.97,2.05L18,15h1.5v-1.64c0,-1.81 -1.6,-3.16 -3.47,-3.16z"/>
</vector>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_service_on" app:showAsAction="always" android:title="Service on" android:icon="@drawable/ic_location_on"/>
<item android:id="@+id/action_service_off" app:showAsAction="always" android:title="Service off" android:icon="@drawable/ic_location_off"/>
</menu>

View File

@ -72,4 +72,19 @@ RecyclerView -> Items der View wiederverwendbar machen
- SQ-Lite implementierung angeschaut aber unser Projekt wird auf Room aufgesetzt
- Room arbeitet mit Annotationen -> viel weniger Code im vergleich zu SQ-Lite
- Network
- HttpsGet Methoden implementiert (waren vorgegeben)
- HttpsGet Methoden implementiert (waren vorgegeben)
## 10.05.2019 - Lab 5
- Unterschied Service - Background Task
- Service: Wenn etwas im Hintergrund laufen muss (z.B. Nuki App)
- BT: z.B Musik Player, immer wenn der User nicht mit der App Interagieren will
Service sollte immmer eine Aktion die er ausführt in einem extra Thread auslagern.
Service wird im Manifest deklariert
Um einen Foregroundservice zu starten muss das innerhalb von 5 Sekunden nach "start Service" (Intent)
Notifications benötigen immer einen Channel (ab Android O verfügbar)