Commit 7959a8fa authored by Paded's avatar Paded

add notification

parent b32a2165
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity
android:launchMode="singleTop"
android:name=".MainActivity" android:name=".MainActivity"
android:windowSoftInputMode="adjustResize" /> android:windowSoftInputMode="adjustResize" />
<activity <activity
...@@ -34,7 +35,7 @@ ...@@ -34,7 +35,7 @@
<activity android:name=".AddBusActivity" /> <activity android:name=".AddBusActivity" />
<activity android:name=".ProfileSettingActivity"></activity> <activity android:name=".ProfileSettingActivity"></activity>
<service <service
android:name=".TrackerService" android:name=".TrackerService3"
android:enabled="true" android:enabled="true"
android:exported="true"></service> android:exported="true"></service>
</application> </application>
......
...@@ -9,13 +9,17 @@ import android.content.Intent; ...@@ -9,13 +9,17 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.Location; import android.location.Location;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import android.os.Process;
import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback; import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationRequest;
...@@ -26,10 +30,18 @@ import com.google.firebase.database.FirebaseDatabase; ...@@ -26,10 +30,18 @@ import com.google.firebase.database.FirebaseDatabase;
public class TrackerService extends Service { public class TrackerService extends Service {
private static final String TAG = "GPS2"; private static final String TAG = "GPS2";
private boolean isRunning = false;
private Looper looper;
private MyServiceHandler myServiceHandler;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate();
HandlerThread handlerthread = new HandlerThread("MyThread", Process.THREAD_PRIORITY_BACKGROUND);
handlerthread.start();
looper = handlerthread.getLooper();
myServiceHandler = new MyServiceHandler(looper);
isRunning = true;
} }
...@@ -37,7 +49,7 @@ public class TrackerService extends Service { ...@@ -37,7 +49,7 @@ public class TrackerService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
String user_id = intent.getStringExtra("user_id"); String user_id = intent.getStringExtra("user_id");
String bus_id = intent.getStringExtra("bus_id"); String bus_id = intent.getStringExtra("bus_id");
buildNotification(); // buildNotification();
requestLocationUpdates(user_id,bus_id); requestLocationUpdates(user_id,bus_id);
Toast.makeText(this, "MyService Started.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "MyService Started.", Toast.LENGTH_SHORT).show();
//If service is killed while starting, it restarts. //If service is killed while starting, it restarts.
...@@ -53,6 +65,37 @@ public class TrackerService extends Service { ...@@ -53,6 +65,37 @@ public class TrackerService extends Service {
return null; return null;
} }
@Override
public void onDestroy() {
isRunning = false;
Toast.makeText(this, "MyService Completed or Stopped.", Toast.LENGTH_SHORT).show();
}
private final class MyServiceHandler extends Handler {
public MyServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
synchronized (this) {
for (int i = 0; i < 10; i++) {
try {
Log.i(TAG, "MyService running...");
Thread.sleep(1000);
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
if(!isRunning){
break;
}
}
}
//stops the service for the start id.
stopSelfResult(msg.arg1);
}
}
private void buildNotification() { private void buildNotification() {
String stop = "stop"; String stop = "stop";
registerReceiver(stopReceiver, new IntentFilter(stop)); registerReceiver(stopReceiver, new IntentFilter(stop));
...@@ -64,7 +107,7 @@ public class TrackerService extends Service { ...@@ -64,7 +107,7 @@ public class TrackerService extends Service {
.setContentText(getString(R.string.notification_text)) .setContentText(getString(R.string.notification_text))
.setOngoing(true) .setOngoing(true)
.setContentIntent(broadcastIntent) .setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.ic_bus); .setSmallIcon(R.drawable.ic_frontal_bus_silhouette);
startForeground(1, builder.build()); startForeground(1, builder.build());
} }
......
package ubontransitdriver.paded.com.ubontransitdriver;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
public class TrackerService2 extends Service {
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 50000;
private static final float LOCATION_DISTANCE = 10;
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
Log.d(TAG, "onLocationChanged Lat Lng: "+location.getLatitude()+" "+location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.PASSIVE_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
LOCATION_INTERVAL,
LOCATION_DISTANCE,
mLocationListeners[0]
);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
/*try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LOCATION_INTERVAL,
LOCATION_DISTANCE,
mLocationListeners[1]
);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}*/
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listener, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
package ubontransitdriver.paded.com.ubontransitdriver;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.Manifest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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 com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Calendar;
import java.util.Date;
public class TrackerService3 extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
FusedLocationProviderClient mFusedLocationClient;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
String TAG = "trackService";
private String user_id;
private String bus_id;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
user_id = intent.getStringExtra("user_id");
bus_id = intent.getStringExtra("bus_id");
Log.d(TAG, "onStartCommand: "+user_id+" "+bus_id);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (mGoogleApiClient != null && mFusedLocationClient != null) {
requestLocationUpdates();
} else {
buildGoogleApiClient();
}
return START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mFusedLocationClient != null) {
Log.d(TAG, "Service Stop");
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
}
public void requestLocationUpdates() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000); // two minute interval
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
}
LocationCallback mLocationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
final String path ="active_bus/"+bus_id+"/"+user_id;
for (Location location : locationResult.getLocations()) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
ref.child("latitude").setValue(location.getLatitude());
ref.child("longitude").setValue(location.getLongitude());
ref.child("time").setValue(location.getTime());
// Date currentTime = Calendar.getInstance().getTime();
// Log.d(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude()+" "+location.getTime()+" "+currentTime);
}
};
};
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/off_green" /> <solid android:color="@color/green" />
<corners android:radius="8dp" /> <corners android:radius="8dp" />
<padding <padding
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/green" /> <solid android:color="@color/white" />
<corners android:radius="8dp" /> <corners android:radius="8dp" />
<stroke
android:width = "1.5dp"
android:color="@color/green"
/>
<padding <padding
android:bottom="7dip" android:bottom="7dip"
android:left="7dip" android:left="7dip"
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
</padding> </padding>
<corners <corners
android:radius="8dp" > android:radius="360dp" >
</corners> </corners>
</shape> </shape>
\ No newline at end of file
<vector android:height="24dp" android:viewportHeight="426.667"
android:viewportWidth="426.667" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/gray_400" android:pathData="M213.333,0C95.467,0 0,95.467 0,213.333s95.467,213.333 213.333,213.333S426.667,331.2 426.667,213.333S331.2,0 213.333,0zM96,149.333l74.667,-74.667l74.667,74.667H192v85.333h-42.667v-85.333H96zM256,352l-74.667,-74.667h53.333V192h42.667v85.333h53.333L256,352z"/>
</vector>
<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="@color/green"
android:pathData="M19.07,4.93l-1.41,1.41C19.1,7.79 20,9.79 20,12c0,4.42 -3.58,8 -8,8s-8,-3.58 -8,-8c0,-4.08 3.05,-7.44 7,-7.93v2.02C8.16,6.57 6,9.03 6,12c0,3.31 2.69,6 6,6s6,-2.69 6,-6c0,-1.66 -0.67,-3.16 -1.76,-4.24l-1.41,1.41C15.55,9.9 16,10.9 16,12c0,2.21 -1.79,4 -4,4s-4,-1.79 -4,-4c0,-1.86 1.28,-3.41 3,-3.86v2.14c-0.6,0.35 -1,0.98 -1,1.72 0,1.1 0.9,2 2,2s2,-0.9 2,-2c0,-0.74 -0.4,-1.38 -1,-1.72V2h-1C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10c0,-2.76 -1.12,-5.26 -2.93,-7.07z"/>
</vector>
<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="@color/white"
android:pathData="M19.07,4.93l-1.41,1.41C19.1,7.79 20,9.79 20,12c0,4.42 -3.58,8 -8,8s-8,-3.58 -8,-8c0,-4.08 3.05,-7.44 7,-7.93v2.02C8.16,6.57 6,9.03 6,12c0,3.31 2.69,6 6,6s6,-2.69 6,-6c0,-1.66 -0.67,-3.16 -1.76,-4.24l-1.41,1.41C15.55,9.9 16,10.9 16,12c0,2.21 -1.79,4 -4,4s-4,-1.79 -4,-4c0,-1.86 1.28,-3.41 3,-3.86v2.14c-0.6,0.35 -1,0.98 -1,1.72 0,1.1 0.9,2 2,2s2,-0.9 2,-2c0,-0.74 -0.4,-1.38 -1,-1.72V2h-1C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10c0,-2.76 -1.12,-5.26 -2.93,-7.07z"/>
</vector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/ic_track_changes_white_24dp" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/ic_track_changes_white_24dp" /> <!-- focused -->
<item android:drawable="@drawable/ic_track_changes_green_24dp" /> <!-- default -->
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/white" />
<item android:color="@color/green"/>
</selector>
\ No newline at end of file
This diff is collapsed.
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<color name="gray_500">#9E9E9E</color> <color name="gray_500">#9E9E9E</color>
<color name="gray_400">#BDBDBD</color> <color name="gray_400">#BDBDBD</color>
<color name="gray_600"> #757575</color> <color name="gray_600"> #757575</color>
<color name="gray_800">#424242</color>
<color name="gray_900">#212121</color> <color name="gray_900">#212121</color>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment