|
|
@ -0,0 +1,351 @@ |
|
|
|
package ru.Service; |
|
|
|
|
|
|
|
import android.app.AlertDialog; |
|
|
|
import android.app.Notification; |
|
|
|
import android.app.NotificationChannel; |
|
|
|
import android.app.NotificationManager; |
|
|
|
import android.app.PendingIntent; |
|
|
|
import android.app.ProgressDialog; |
|
|
|
import android.app.Service; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.location.Location; |
|
|
|
import android.location.LocationManager; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.IBinder; |
|
|
|
import android.provider.Settings; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.core.app.ActivityCompat; |
|
|
|
import androidx.core.app.NotificationCompat; |
|
|
|
|
|
|
|
import ru.visionlab.femdemo.CheckInActivity; |
|
|
|
import ru.visionlab.femdemo.R; |
|
|
|
|
|
|
|
public class MyLocationService extends Service { |
|
|
|
|
|
|
|
LocationManager locationManager; |
|
|
|
private static final int REQUEST_LOCATION = 1; |
|
|
|
String latitude, longitude; |
|
|
|
ProgressDialog progressDialog; |
|
|
|
|
|
|
|
|
|
|
|
Handler handler = new Handler(); |
|
|
|
Runnable runnable; |
|
|
|
int delay = 10000; |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
// execution of service will start |
|
|
|
// on calling this method |
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) { |
|
|
|
|
|
|
|
System.out.println("Inside my location service"); |
|
|
|
|
|
|
|
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
|
|
|
addNotification(); |
|
|
|
trackGPS(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// returns the status |
|
|
|
// of the program |
|
|
|
return START_STICKY; |
|
|
|
} |
|
|
|
|
|
|
|
private void trackGPS() { |
|
|
|
handler.postDelayed(runnable = new Runnable() { |
|
|
|
public void run() { |
|
|
|
handler.postDelayed(runnable, delay); |
|
|
|
System.out.println("Inside handler"); |
|
|
|
/*Intent intent = new Intent(MainActivity.this,MyLocationService.class); |
|
|
|
startService(intent);*/ |
|
|
|
|
|
|
|
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { |
|
|
|
OnGPS(); |
|
|
|
} else { |
|
|
|
getLocation(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}, delay); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
// execution of the service will |
|
|
|
// stop on calling this method |
|
|
|
public void onDestroy() { |
|
|
|
handler.removeCallbacks(runnable); |
|
|
|
super.onDestroy(); |
|
|
|
} |
|
|
|
private void addNotification() { |
|
|
|
|
|
|
|
System.out.println("Inside addNotification"); |
|
|
|
|
|
|
|
NotificationManager mNotificationManager; |
|
|
|
|
|
|
|
NotificationCompat.Builder mBuilder = |
|
|
|
new NotificationCompat.Builder(getApplicationContext(), "notify_001"); |
|
|
|
Intent ii = new Intent(getApplicationContext(), CheckInActivity.class); |
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, PendingIntent.FLAG_IMMUTABLE); |
|
|
|
|
|
|
|
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); |
|
|
|
|
|
|
|
bigText.setBigContentTitle("Service started "); |
|
|
|
bigText.setSummaryText("Location getting tracked "); |
|
|
|
|
|
|
|
mBuilder.setContentIntent(pendingIntent); |
|
|
|
mBuilder.setSmallIcon(R.mipmap.ic_launcher); |
|
|
|
mBuilder.setContentTitle("Service started"); |
|
|
|
mBuilder.setContentText("Location getting tracked"); |
|
|
|
mBuilder.setPriority(Notification.PRIORITY_MAX); |
|
|
|
mBuilder.setStyle(bigText); |
|
|
|
|
|
|
|
mNotificationManager = |
|
|
|
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
// === Removed some obsoletes |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) |
|
|
|
{ |
|
|
|
String channelId = "Your_channel_id"; |
|
|
|
NotificationChannel channel = new NotificationChannel( |
|
|
|
channelId, |
|
|
|
"Channel human readable title", |
|
|
|
NotificationManager.IMPORTANCE_HIGH); |
|
|
|
mNotificationManager.createNotificationChannel(channel); |
|
|
|
mBuilder.setChannelId(channelId); |
|
|
|
} |
|
|
|
|
|
|
|
mNotificationManager.notify(0, mBuilder.build()); |
|
|
|
System.out.println("Inside add notific"); |
|
|
|
/*NotificationCompat.Builder builder = |
|
|
|
new NotificationCompat.Builder(this) |
|
|
|
.setSmallIcon(R.drawable.ic_launcher_background) |
|
|
|
.setContentTitle("Notifications Example") |
|
|
|
.setContentText("This is a test notification") |
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT); |
|
|
|
|
|
|
|
Intent notificationIntent = new Intent(this, MainActivity.class); |
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, |
|
|
|
PendingIntent.FLAG_IMMUTABLE); |
|
|
|
builder.setContentIntent(contentIntent); |
|
|
|
|
|
|
|
// Add as notification |
|
|
|
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
manager.notify(0, builder.build());*/ |
|
|
|
} |
|
|
|
|
|
|
|
private void locationNotification() { |
|
|
|
|
|
|
|
System.out.println("Inside addNotification"); |
|
|
|
|
|
|
|
NotificationManager mNotificationManager; |
|
|
|
|
|
|
|
NotificationCompat.Builder mBuilder = |
|
|
|
new NotificationCompat.Builder(getApplicationContext(), "notify_001"); |
|
|
|
Intent ii = new Intent(getApplicationContext(), CheckInActivity.class); |
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, PendingIntent.FLAG_IMMUTABLE); |
|
|
|
|
|
|
|
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); |
|
|
|
|
|
|
|
bigText.setBigContentTitle("Location notification"); |
|
|
|
bigText.setSummaryText("Entered location perimeter"); |
|
|
|
|
|
|
|
mBuilder.setContentIntent(pendingIntent); |
|
|
|
mBuilder.setSmallIcon(R.mipmap.ic_launcher); |
|
|
|
mBuilder.setContentTitle("Service started"); |
|
|
|
mBuilder.setContentText("Location getting tracked"); |
|
|
|
mBuilder.setPriority(Notification.PRIORITY_MAX); |
|
|
|
mBuilder.setStyle(bigText); |
|
|
|
|
|
|
|
mNotificationManager = |
|
|
|
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
// === Removed some obsoletes |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) |
|
|
|
{ |
|
|
|
String channelId = "Your_channel_id"; |
|
|
|
NotificationChannel channel = new NotificationChannel( |
|
|
|
channelId, |
|
|
|
"Channel human readable title", |
|
|
|
NotificationManager.IMPORTANCE_HIGH); |
|
|
|
mNotificationManager.createNotificationChannel(channel); |
|
|
|
mBuilder.setChannelId(channelId); |
|
|
|
} |
|
|
|
|
|
|
|
mNotificationManager.notify(0, mBuilder.build()); |
|
|
|
System.out.println("Inside add notific"); |
|
|
|
/*NotificationCompat.Builder builder = |
|
|
|
new NotificationCompat.Builder(this) |
|
|
|
.setSmallIcon(R.drawable.ic_launcher_background) |
|
|
|
.setContentTitle("Notifications Example") |
|
|
|
.setContentText("This is a test notification") |
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT); |
|
|
|
|
|
|
|
Intent notificationIntent = new Intent(this, MainActivity.class); |
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, |
|
|
|
PendingIntent.FLAG_IMMUTABLE); |
|
|
|
builder.setContentIntent(contentIntent); |
|
|
|
|
|
|
|
// Add as notification |
|
|
|
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
manager.notify(0, builder.build());*/ |
|
|
|
} |
|
|
|
|
|
|
|
private void getLocation() { |
|
|
|
//Check Permissions again |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
// 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; |
|
|
|
} |
|
|
|
Location LocationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
|
|
|
Location LocationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); |
|
|
|
Location LocationPassive=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); |
|
|
|
|
|
|
|
if (LocationGps !=null) |
|
|
|
{ |
|
|
|
double lat=LocationGps.getLatitude(); |
|
|
|
double longi=LocationGps.getLongitude(); |
|
|
|
/*double newLat = 22.5747; |
|
|
|
double newLong = 88.4338;*/ |
|
|
|
|
|
|
|
double newLat = 22.5796; |
|
|
|
double newLong = 88.4383; |
|
|
|
|
|
|
|
float[] results = new float[1]; |
|
|
|
Location.distanceBetween(lat,longi,newLat,newLong,results); |
|
|
|
float distance = results[0]; |
|
|
|
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show(); |
|
|
|
if(distance > 400){ |
|
|
|
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show(); |
|
|
|
System.out.println("You are outside location range"); |
|
|
|
System.out.println("Position 1 "); |
|
|
|
//moreThanFour(); |
|
|
|
} |
|
|
|
else{ |
|
|
|
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show(); |
|
|
|
locationNotification(); |
|
|
|
System.out.println("You are inside location range"); |
|
|
|
System.out.println("Position 2 "); |
|
|
|
//lessThanFour(); |
|
|
|
} |
|
|
|
latitude=String.valueOf(lat); |
|
|
|
longitude=String.valueOf(longi); |
|
|
|
|
|
|
|
latitude=String.valueOf(lat); |
|
|
|
longitude=String.valueOf(longi); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Inside getLocation"); |
|
|
|
|
|
|
|
} |
|
|
|
else if (LocationNetwork !=null) |
|
|
|
{ |
|
|
|
System.out.println("Position 3 "); |
|
|
|
double lat=LocationNetwork.getLatitude(); |
|
|
|
double longi=LocationNetwork.getLongitude(); |
|
|
|
|
|
|
|
double newLat = 22.5747; |
|
|
|
double newLong = 88.4338; |
|
|
|
|
|
|
|
float[] results = new float[1]; |
|
|
|
Location.distanceBetween(lat,longi,newLat,newLong,results); |
|
|
|
float distance = results[0]; |
|
|
|
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show(); |
|
|
|
if(distance > 400){ |
|
|
|
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show(); |
|
|
|
System.out.println("You are outside location range"); |
|
|
|
//moreThanFour(); |
|
|
|
} |
|
|
|
else{ |
|
|
|
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show(); |
|
|
|
System.out.println("You are inside location range"); |
|
|
|
//lessThanFour(); |
|
|
|
} |
|
|
|
latitude=String.valueOf(lat); |
|
|
|
longitude=String.valueOf(longi); |
|
|
|
|
|
|
|
latitude=String.valueOf(lat); |
|
|
|
longitude=String.valueOf(longi); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
else if (LocationPassive !=null) |
|
|
|
{ |
|
|
|
double lat=LocationPassive.getLatitude(); |
|
|
|
double longi=LocationPassive.getLongitude(); |
|
|
|
|
|
|
|
latitude=String.valueOf(lat); |
|
|
|
longitude=String.valueOf(longi); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// Toast.makeText(this, "Can't Get Your Location", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
|
|
double lat=LocationNetwork.getLatitude(); |
|
|
|
double longi=LocationNetwork.getLongitude(); |
|
|
|
|
|
|
|
double newLat = 22.5747; |
|
|
|
double newLong = 88.4338; |
|
|
|
|
|
|
|
float[] results = new float[1]; |
|
|
|
Location.distanceBetween(lat,longi,newLat,newLong,results); |
|
|
|
float distance = results[0]; |
|
|
|
Toast.makeText(this,String.valueOf(distance)+" metres from location",Toast.LENGTH_LONG).show(); |
|
|
|
if(distance > 400){ |
|
|
|
Toast.makeText(this,"You are outside location range",Toast.LENGTH_LONG).show(); |
|
|
|
System.out.println("You are outside location range"); |
|
|
|
//moreThanFour(); |
|
|
|
} |
|
|
|
else{ |
|
|
|
Toast.makeText(this,"You are inside location range",Toast.LENGTH_LONG).show(); |
|
|
|
System.out.println("You are inside location range"); |
|
|
|
//lessThanFour(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Thats All Run Your App |
|
|
|
} |
|
|
|
|
|
|
|
private void OnGPS() { |
|
|
|
|
|
|
|
final AlertDialog.Builder builder= new AlertDialog.Builder(this); |
|
|
|
|
|
|
|
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); |
|
|
|
} |
|
|
|
}).setNegativeButton("NO", new DialogInterface.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
|
|
|
|
dialog.cancel(); |
|
|
|
} |
|
|
|
}); |
|
|
|
final AlertDialog alertDialog=builder.create(); |
|
|
|
alertDialog.show(); |
|
|
|
System.out.println("Inside OnGPS"); |
|
|
|
} |
|
|
|
@Nullable |
|
|
|
@Override |
|
|
|
public IBinder onBind(Intent intent) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |