1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
public class MyService extends Service implements LocationListener {
private LocationManager lm;
@Override
public void onCreate() {
super.onCreate();
Log.i("addLocationListener", "function is RUNNING!");
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
if (ActivityCompat.checkSelfPermission(getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), android.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;
}
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 0, this, Looper.getMainLooper());
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 0, this, Looper.getMainLooper());
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//return mBinder;
return messenger.getBinder();
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(ctx, "Location changed", Toast.LENGTH_SHORT).show();
//do something.. when the location has changed
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
public static boolean locationFounded() {
return locationFounded;
}
public void setLocationFounded(boolean lf) {
locationFounded = lf;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stoped", Toast.LENGTH_SHORT).show();
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case MSG_GET_COUNT:
try{
messenger.send(msg);
}catch (RemoteException e)
{
Log.i("Messenger What","Test Messenger What");
}
break;
default:
super.handleMessage(msg);
}
}
};
private Messenger messenger = new Messenger(handler); |
Partager