mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-07 21:01:03 +00:00
Implement tracking as background service in a separate process
This commit is contained in:
1 parent
5f7fd13e7e
commit
3d878d561e
8 files changed
+122
-87
No files matched your search
@@ -2,7 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.sap.sailing.android.tracking.app"
|
||||
android:versionCode="1"
|
||||
android:versionName="1 - Altitude">
|
||||
android:versionName="1 - Altitude" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="13"
|
||||
@@ -20,34 +20,36 @@
|
||||
android:icon="@drawable/sap_sailing_app_icon"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/Theme.Welter" >
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version" />
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version" />
|
||||
|
||||
<activity
|
||||
android:name=".ui.activities.LaunchActivity"
|
||||
android:launchMode="standard"
|
||||
android:finishOnTaskLaunch="true" >
|
||||
android:finishOnTaskLaunch="true"
|
||||
android:launchMode="standard" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="com.sap.sailing.android.tracking.app.ui.activities.SystemInformationActivity">
|
||||
<activity android:name="com.sap.sailing.android.tracking.app.ui.activities.SystemInformationActivity" >
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="com.sap.sailing.android.tracking.app.ui.activities.SettingsActivity">
|
||||
<activity android:name="com.sap.sailing.android.tracking.app.ui.activities.SettingsActivity" >
|
||||
</activity>
|
||||
<activity android:name="com.sap.sailing.android.tracking.app.ui.activities.StopTrackingActivity" >
|
||||
</activity>
|
||||
|
||||
<service
|
||||
android:name="com.sap.sailing.android.tracking.app.services.TrackingService"
|
||||
android:exported="false" >
|
||||
android:exported="false"
|
||||
android:process=":tracking_process"
|
||||
android:icon="@drawable/icon"
|
||||
android:label="@string/tracking_service"
|
||||
>
|
||||
</service>
|
||||
|
||||
|
||||
<service
|
||||
android:name="com.sap.sailing.android.shared.services.sending.MessageSendingService"
|
||||
android:exported="false" >
|
||||
@@ -56,6 +58,7 @@
|
||||
<action android:name="com.sap.sailing.android.tracking.app.action.sendMessage" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
|
||||
<receiver android:name="com.sap.sailing.android.shared.services.sending.ConnectivityChangedReceiver" >
|
||||
<intent-filter>
|
||||
@@ -64,4 +67,4 @@
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
</manifest>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -4,10 +4,16 @@
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/status"
|
||||
<Button
|
||||
android:id="@+id/btnStartTracking"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/not_tracking" />
|
||||
android:text="@string/start_tracking" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnNextActivity"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/next_activity" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnStopTracking"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/stop_tracking" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -13,4 +13,12 @@
|
||||
<string name="device_identifier">Device Identifier</string>
|
||||
|
||||
<string name="play_services_available">Google Play services is available</string>
|
||||
|
||||
<string name="tracking_service">Tracking Service</string>
|
||||
|
||||
<string name="stop_tracking">Stop Tracking</string>
|
||||
|
||||
<string name="start_tracking">Start Tracking</string>
|
||||
|
||||
<string name="next_activity">Next Activity</string>
|
||||
</resources>
|
||||
+10
-16
@@ -5,7 +5,6 @@ import java.util.UUID;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.os.Binder;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
|
||||
@@ -23,6 +22,7 @@ import com.sap.sailing.android.tracking.app.nmea.NmeaGprmcBuilder;
|
||||
|
||||
public class TrackingService extends Service implements ConnectionCallbacks, OnConnectionFailedListener,
|
||||
LocationListener {
|
||||
|
||||
private LocationClient locationClient;
|
||||
private LocationRequest locationRequest;
|
||||
private boolean locationUpdateRequested = false;
|
||||
@@ -44,22 +44,11 @@ LocationListener {
|
||||
|
||||
locationClient = new LocationClient(this, this, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for clients to access. Because we know this service always runs in the same process as its clients, we
|
||||
* don't need to deal with IPC.
|
||||
*/
|
||||
public class TrackingServiceBinder extends Binder {
|
||||
public TrackingService getService() {
|
||||
return TrackingService.this;
|
||||
}
|
||||
}
|
||||
|
||||
private final TrackingServiceBinder binder = new TrackingServiceBinder();
|
||||
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return binder;
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
startTracking();
|
||||
return Service.START_STICKY;
|
||||
}
|
||||
|
||||
public void startTracking() {
|
||||
@@ -108,4 +97,9 @@ LocationListener {
|
||||
ExLog.i(this, TAG, "Sent NMEA to server:" + nmea);
|
||||
//TODO also store to SD card
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+32
-52
@@ -1,76 +1,55 @@
|
||||
package com.sap.sailing.android.tracking.app.ui.activities;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.widget.TextView;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.GooglePlayServicesUtil;
|
||||
import com.sap.sailing.android.shared.logging.ExLog;
|
||||
import com.sap.sailing.android.tracking.app.R;
|
||||
import com.sap.sailing.android.tracking.app.services.TrackingService;
|
||||
import com.sap.sailing.android.tracking.app.services.TrackingService.TrackingServiceBinder;
|
||||
|
||||
public class LaunchActivity extends BaseActivity {
|
||||
private static final String TAG = LaunchActivity.class.getName();
|
||||
|
||||
private final TrackingServiceConnection trackingServiceConnection = new TrackingServiceConnection();
|
||||
private TrackingService trackingService;
|
||||
private boolean boundTrackingService = false;
|
||||
private TextView status;
|
||||
|
||||
private class TrackingServiceConnection implements ServiceConnection {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
TrackingServiceBinder binder = (TrackingServiceBinder) service;
|
||||
trackingService = binder.getService();
|
||||
trackingService.startTracking();
|
||||
boundTrackingService = true;
|
||||
status.setText(R.string.tracking);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName arg) {
|
||||
boundTrackingService = false;
|
||||
}
|
||||
}
|
||||
private Button startTracking;
|
||||
private Button nextActivity;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.launch_activity);
|
||||
status = (TextView) findViewById(R.id.status);
|
||||
|
||||
startTracking = (Button) findViewById(R.id.btnStartTracking);
|
||||
startTracking.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (!googlePLayServicesAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent startTracking = new Intent(LaunchActivity.this, TrackingService.class);
|
||||
getBaseContext().startService(startTracking);
|
||||
}
|
||||
});
|
||||
|
||||
nextActivity = (Button) findViewById(R.id.btnNextActivity);
|
||||
nextActivity.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent intent = new Intent(LaunchActivity.this, StopTrackingActivity.class);
|
||||
fadeActivity(intent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (servicesConnected()){
|
||||
bindService(new Intent(getApplicationContext(), TrackingService.class), trackingServiceConnection,
|
||||
Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
}
|
||||
private boolean googlePLayServicesAvailable() {
|
||||
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (boundTrackingService) {
|
||||
trackingService.stopTracking();
|
||||
status.setText(R.string.not_tracking);
|
||||
unbindService(trackingServiceConnection);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean servicesConnected() {
|
||||
int resultCode =
|
||||
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
|
||||
|
||||
if (ConnectionResult.SUCCESS == resultCode) {
|
||||
ExLog.i(this, TAG, getString(R.string.play_services_available));
|
||||
return true;
|
||||
@@ -85,7 +64,7 @@ public class LaunchActivity extends BaseActivity {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ErrorDialogFragment extends DialogFragment {
|
||||
private Dialog dialog;
|
||||
|
||||
@@ -97,7 +76,8 @@ public class LaunchActivity extends BaseActivity {
|
||||
/**
|
||||
* Set the dialog to display
|
||||
*
|
||||
* @param dialog An error dialog
|
||||
* @param dialog
|
||||
* An error dialog
|
||||
*/
|
||||
public void setDialog(Dialog dialog) {
|
||||
this.dialog = dialog;
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.sap.sailing.android.tracking.app.ui.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.sap.sailing.android.tracking.app.R;
|
||||
import com.sap.sailing.android.tracking.app.services.TrackingService;
|
||||
|
||||
public class StopTrackingActivity extends BaseActivity {
|
||||
|
||||
private Button stopTracking;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.stop_tracking_activity);
|
||||
stopTracking = (Button) findViewById(R.id.btnStopTracking);
|
||||
stopTracking.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent stopTracking = new Intent(StopTrackingActivity.this, TrackingService.class);
|
||||
getBaseContext().stopService(stopTracking);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user