add shit!!!!!!!!!!
add LocationService edit Overview Next Step: Fill up the Position from the Location Service
This commit is contained in:
parent
6b4b69ad1d
commit
5027665009
|
@ -19,11 +19,17 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||
implementation 'com.android.support:appcompat-v7:28.0.0'
|
||||
implementation 'com.android.support:support-media-compat:28.0.0'
|
||||
implementation 'com.android.support:design:28.0.0'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||
implementation 'com.android.support:support-v4:28.0.0'
|
||||
implementation 'android.arch.persistence.room:runtime:1.1.1'
|
||||
implementation 'com.google.android.gms:play-services-location:16.0.0'
|
||||
implementation 'com.google.android.gms:play-services-maps:16.1.0'
|
||||
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
implementation 'com.google.maps.android:android-maps-utils:0.5+'
|
||||
}
|
||||
|
|
|
@ -2,13 +2,22 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="at.fhj.swd.dailyhelper">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:name=".DailyHelperApplikation"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key"/>
|
||||
|
||||
<activity
|
||||
android:name=".ui.activity.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
|
@ -19,6 +28,8 @@
|
|||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<service android:name=".service.LocationService">
|
||||
</service>
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -0,0 +1,24 @@
|
|||
package at.fhj.swd.dailyhelper;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.os.Build;
|
||||
|
||||
public class DailyHelperApplikation extends Application {
|
||||
|
||||
public static final String CHANNEL_ID = "location_service_channel";
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
package at.fhj.swd.dailyhelper.service;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Notification;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.IBinder;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.gms.location.FusedLocationProviderClient;
|
||||
import com.google.android.gms.location.LocationCallback;
|
||||
import com.google.android.gms.location.LocationRequest;
|
||||
import com.google.android.gms.location.LocationResult;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
|
||||
import at.fhj.swd.dailyhelper.R;
|
||||
import at.fhj.swd.dailyhelper.ui.activity.MainActivity;
|
||||
|
||||
import static at.fhj.swd.dailyhelper.DailyHelperApplikation.CHANNEL_ID;
|
||||
|
||||
public class LocationService extends Service {
|
||||
|
||||
private static final String TAG = "LocationService";
|
||||
|
||||
private Notification notification;
|
||||
|
||||
private FusedLocationProviderClient fusedLocationProviderClient;
|
||||
private LocationCallback locationCallback;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
setupNotification();
|
||||
Log.d(TAG, "service created");
|
||||
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
|
||||
locationCallback = new LocationCallback() {
|
||||
@Override
|
||||
public void onLocationResult(LocationResult locationResult) {
|
||||
if(locationResult.getLocations().size() != 0){
|
||||
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LocationService.this);
|
||||
preferences.edit()
|
||||
.putFloat("latitude", (float) locationResult.getLocations().get(0).getLatitude())
|
||||
.putFloat("longitude", (float) locationResult.getLocations().get(0).getLongitude())
|
||||
.putFloat("altitude", (float) locationResult.getLocations().get(0).getAltitude())
|
||||
.putFloat("accuracy", (float) locationResult.getLocations().get(0).getAccuracy())
|
||||
.putFloat("speed", locationResult.getLocations().get(0).getSpeed()).apply();
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public void startLocationUpdate() {
|
||||
LocationRequest request = new LocationRequest();
|
||||
request.setInterval(5000);
|
||||
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
fusedLocationProviderClient.requestLocationUpdates(request, locationCallback, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void stopLocationUpdate() {
|
||||
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.d(TAG, "service started");
|
||||
startForeground(4545, notification);
|
||||
startLocationUpdate();
|
||||
return START_STICKY;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
stopLocationUpdate();
|
||||
super.onDestroy();
|
||||
Log.d(TAG, "service destroyed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setupNotification() {
|
||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
|
||||
|
||||
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentText("Updating location")
|
||||
.setContentTitle("DailyHelper Location Service")
|
||||
.setSmallIcon(R.drawable.ic_star)
|
||||
.setContentIntent(pendingIntent).build();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,11 @@
|
|||
package at.fhj.swd.dailyhelper.ui.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v7.app.ActionBarDrawerToggle;
|
||||
import android.view.MenuItem;
|
||||
|
@ -12,6 +17,7 @@ import android.support.v7.widget.Toolbar;
|
|||
import android.view.Menu;
|
||||
|
||||
import at.fhj.swd.dailyhelper.R;
|
||||
import at.fhj.swd.dailyhelper.service.LocationService;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.HomeFragment;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.MapFragment;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.WeatherFragment;
|
||||
|
@ -57,6 +63,30 @@ public class MainActivity extends AppCompatActivity
|
|||
}
|
||||
}
|
||||
|
||||
private void startLocationService(){
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
Intent intent = new Intent(this, LocationService.class);
|
||||
startService(intent);
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 12435 );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
if(requestCode == 12435){
|
||||
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
|
||||
startLocationService();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void stopLocationService(){
|
||||
Intent intent = new Intent(this, LocationService.class);
|
||||
stopService(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
|
@ -64,20 +94,6 @@ public class MainActivity extends AppCompatActivity
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
// automatically handle clicks on the Home/Up button, so long
|
||||
// as you specify a parent activity in AndroidManifest.xml.
|
||||
int id = item.getItemId();
|
||||
|
||||
//noinspection SimplifiableIfStatement
|
||||
if (id == R.id.action_settings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(MenuItem item) {
|
||||
|
@ -98,4 +114,27 @@ public class MainActivity extends AppCompatActivity
|
|||
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;
|
||||
|
||||
case R.id.action_refresh:
|
||||
// fetchLocations();
|
||||
return true;
|
||||
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<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>
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<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>
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
|
||||
</vector>
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M20,15.31L23.31,12 20,8.69V4h-4.69L12,0.69 8.69,4H4v4.69L0.69,12 4,15.31V20h4.69L12,23.31 15.31,20H20v-4.69zM12,18V6c3.31,0 6,2.69 6,6s-2.69,6 -6,6z"/>
|
||||
</vector>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<stroke
|
||||
android:width="5dip"
|
||||
android:color="@android:color/white" />
|
||||
</shape>
|
|
@ -2,12 +2,87 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@android:color/holo_blue_light">
|
||||
|
||||
<TextView
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ly_position"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="Übersicht, Tempertur, Position, Geschwindigkeit, Wetter"
|
||||
android:textSize="80px"/>
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_margin="20dp"
|
||||
android:padding="20dp"
|
||||
android:background="@drawable/border">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_position"
|
||||
android:text="Position"
|
||||
android:padding="10dp"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_longitude"
|
||||
android:text="Breitengrag"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_latitude"
|
||||
android:text="Längengrad"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_speed"
|
||||
android:text="Geschwindigkeit"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_altitude"
|
||||
android:text="Seehöhe"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_accuracy"
|
||||
android:text="Genauigkeit"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ly_wheater"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_below="@+id/ly_position"
|
||||
android:layout_margin="20dp"
|
||||
android:padding="20dp"
|
||||
android:background="@drawable/border">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_wheather"
|
||||
android:text="Wetter"
|
||||
android:padding="10dp"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
</RelativeLayout>
|
|
@ -1,9 +1,9 @@
|
|||
<?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_settings"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/action_settings"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item android:id="@+id/action_service_on" android:title="Service on" app:showAsAction="always" android:icon="@drawable/ic_location_on"/>
|
||||
<item android:id="@+id/action_service_off" android:title="Service off" app:showAsAction="always" android:icon="@drawable/ic_location_off"/>
|
||||
<item android:id="@+id/action_refresh" android:title="Refresh" app:showAsAction="always" android:icon="@drawable/ic_refresh_black_24dp"/>
|
||||
|
||||
</menu>
|
||||
|
|
|
@ -12,4 +12,7 @@
|
|||
<string name="menu_map">Karte</string>
|
||||
<string name="menu_level">Wasserwaage</string>
|
||||
|
||||
|
||||
<string name="google_maps_key">AIzaSyBQhcmDlyP01yNwTd4RTlatQIAJH3wJHw0</string>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Reference in New Issue