Add background task
This commit is contained in:
parent
2f802ad64f
commit
355c78de5d
@ -42,6 +42,9 @@
|
||||
android:label="@string/title_activity_login"
|
||||
android:noHistory="true">
|
||||
</activity>
|
||||
<service
|
||||
android:name=".service.GoogleService"
|
||||
android:exported="false" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -1,27 +1,36 @@
|
||||
package com.uam.wmi.findmytutor.activity;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Geocoder;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.BottomNavigationView;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.uam.wmi.findmytutor.R;
|
||||
import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
import com.uam.wmi.findmytutor.network.ApiClient;
|
||||
import com.uam.wmi.findmytutor.service.CoordinateService;
|
||||
import com.uam.wmi.findmytutor.service.GoogleService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Address;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
@ -33,19 +42,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
private NotificationFragment notificationFragment;
|
||||
private ProfileFragment profileFragment;
|
||||
|
||||
|
||||
|
||||
private MapView mapView;
|
||||
|
||||
public List<Coordinate> getCoordinates() {
|
||||
return this.coordinates;
|
||||
}
|
||||
|
||||
public void setCoordinates(List<Coordinate> coordinates) {
|
||||
this.coordinates = coordinates;
|
||||
}
|
||||
|
||||
public List<Coordinate> coordinates;
|
||||
private static final int REQUEST_PERMISSIONS = 100;
|
||||
boolean boolean_permission;
|
||||
SharedPreferences mPref;
|
||||
SharedPreferences.Editor medit;
|
||||
Double latitude, longitude;
|
||||
Geocoder geocoder;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -53,10 +55,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
Mapbox.getInstance(this, getString(R.string.access_token));
|
||||
setContentView(R.layout.activity_main);
|
||||
Bundle extras = getIntent().getExtras();
|
||||
|
||||
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
|
||||
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
|
||||
isTutor = (boolean) extras.get("is_tutor");
|
||||
if (!isTutor){
|
||||
|
||||
geocoder = new Geocoder(this, Locale.getDefault());
|
||||
mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
medit = mPref.edit();
|
||||
|
||||
if (!isTutor) {
|
||||
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@ -92,23 +100,32 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
// Logout button
|
||||
final FloatingActionButton button = findViewById(R.id.logoutButton);
|
||||
button.setOnClickListener(new View.OnClickListener(){
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
SharedPreferences preferences = getSharedPreferences("fmtPrefs",Context.MODE_PRIVATE);
|
||||
SharedPreferences preferences = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.clear().commit();
|
||||
|
||||
Intent i = getBaseContext().getPackageManager()
|
||||
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
|
||||
.getLaunchIntentForPackage(getBaseContext().getPackageName());
|
||||
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
startActivity(i);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
// CoordinateService service = ApiClient.getClient(getApplicationContext())
|
||||
// .create(CoordinateService.class);
|
||||
|
||||
|
||||
//Start background task
|
||||
fn_permission();
|
||||
|
||||
if (boolean_permission) {
|
||||
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
|
||||
startService(intent);
|
||||
} else {
|
||||
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -118,5 +135,68 @@ public class MainActivity extends AppCompatActivity {
|
||||
fragmentTransaction.replace(R.id.main_frame, fragment);
|
||||
fragmentTransaction.commit();
|
||||
}
|
||||
|
||||
|
||||
private void fn_permission() {
|
||||
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
|
||||
|
||||
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
|
||||
|
||||
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||
|
||||
},
|
||||
REQUEST_PERMISSIONS);
|
||||
|
||||
}
|
||||
} else {
|
||||
boolean_permission = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
|
||||
switch (requestCode) {
|
||||
case REQUEST_PERMISSIONS: {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
boolean_permission = true;
|
||||
|
||||
} else {
|
||||
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
latitude = Double.valueOf(intent.getStringExtra("latutide"));
|
||||
longitude = Double.valueOf(intent.getStringExtra("longitude"));
|
||||
|
||||
List<Address> addresses = null;
|
||||
|
||||
try {
|
||||
addresses = geocoder.getFromLocation(latitude, longitude, 1);
|
||||
String cityName = addresses.get(0).getAddressLine(0);
|
||||
String stateName = addresses.get(0).getAddressLine(1);
|
||||
String countryName = addresses.get(0).getAddressLine(2);
|
||||
|
||||
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
Log.e("Coords", String.valueOf(latitude));
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
@ -4,10 +4,14 @@ import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.DELETE;
|
||||
import retrofit2.http.Field;
|
||||
import retrofit2.http.FormUrlEncoded;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.PUT;
|
||||
@ -33,8 +37,17 @@ public interface CoordinateService {
|
||||
@GET("api/coordinates/top/online")
|
||||
Call<List<Coordinate>> getOnlineCoordinates();
|
||||
|
||||
@POST("api/coordinates")
|
||||
Call<ResponseBody> postCoordinate(@Body Coordinate coordinate);
|
||||
/* @POST("api/coordinates")
|
||||
Call<Coordinate> postCoordinate(@Body Coordinate coordinate);*/
|
||||
|
||||
@POST("postCoordinate")
|
||||
@FormUrlEncoded
|
||||
Single<Coordinate> postCoordinate(
|
||||
@Field("longitude") Float longitude,
|
||||
@Field("latitude") Float latitude,
|
||||
@Field("label") String label,
|
||||
@Field("userId") String userId
|
||||
);
|
||||
|
||||
@PUT("api/coordinates/{id}")
|
||||
Call<Coordinate> putCoordinatesById(@Path("id") String id);
|
||||
|
@ -0,0 +1,170 @@
|
||||
package com.uam.wmi.findmytutor.service;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
import com.uam.wmi.findmytutor.model.User;
|
||||
import com.uam.wmi.findmytutor.network.ApiClient;
|
||||
import com.uam.wmi.findmytutor.utils.PrefUtils;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.observers.DisposableSingleObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class GoogleService extends Service implements LocationListener {
|
||||
|
||||
boolean isGPSEnable = false;
|
||||
boolean isNetworkEnable = false;
|
||||
double latitude, longitude;
|
||||
LocationManager locationManager;
|
||||
Location location;
|
||||
private Handler mHandler = new Handler();
|
||||
private Timer mTimer = null;
|
||||
long notify_interval = 1000;
|
||||
public static String str_receiver = "servicetutorial.service.receiver";
|
||||
Intent intent;
|
||||
|
||||
private CoordinateService apiService;
|
||||
private CompositeDisposable disposable = new CompositeDisposable();
|
||||
|
||||
|
||||
public GoogleService() {
|
||||
apiService = ApiClient.getClient(getApplicationContext()).create(CoordinateService.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
mTimer = new Timer();
|
||||
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
|
||||
intent = new Intent(str_receiver);
|
||||
}
|
||||
|
||||
@SuppressLint("LongLogTag")
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
Log.e("Background location has changed", String.valueOf(location));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
|
||||
}
|
||||
|
||||
private void fn_getlocation() {
|
||||
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
|
||||
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
||||
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
||||
|
||||
if (!isGPSEnable && !isNetworkEnable) {
|
||||
|
||||
} else {
|
||||
|
||||
if (isNetworkEnable) {
|
||||
location = null;
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
// int[] grantResults)
|
||||
// to handle the case where the user grants the permission. See the documentation
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
|
||||
if (locationManager!=null){
|
||||
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
if (location!=null){
|
||||
|
||||
Log.e("latitude",location.getLatitude()+"");
|
||||
Log.e("longitude",location.getLongitude()+"");
|
||||
|
||||
latitude = location.getLatitude();
|
||||
longitude = location.getLongitude();
|
||||
fn_update(location);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (isGPSEnable){
|
||||
location = null;
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
|
||||
if (locationManager!=null){
|
||||
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
if (location!=null){
|
||||
Log.e("latitude",location.getLatitude()+"");
|
||||
Log.e("longitude",location.getLongitude()+"");
|
||||
latitude = location.getLatitude();
|
||||
longitude = location.getLongitude();
|
||||
fn_update(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class TimerTaskToGetLocation extends TimerTask{
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
mHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fn_getlocation();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void fn_update(Location location){
|
||||
|
||||
intent.putExtra("latutide",location.getLatitude()+"");
|
||||
intent.putExtra("longitude",location.getLongitude()+"");
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user