Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Location Updates on Given interval via Pending Intent and Alarm manager. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion LocationWorker/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".AlarmReceiver" >
</receiver>

</application>



</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.pratikbutani.workerexample.alram;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

private Context context;
private String currentLocality = "";
private String currentLocation = "";
private Intent i;

@Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
this.context = context;
//Log.d("AlarmReceiver","OnReceive()================");
this.i = intent;

getImeiLocation();

}

public void getImeiLocation() {

LocationManager locationManager = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
try{
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

} catch (SecurityException unlikely) {
Log.e("", "Lost location permission." + unlikely);
}
}


/*---------- Listener class to get coordinates ------------- */
private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
String longitude = "Longitude: " + loc.getLongitude();
String latitude = "Latitude: " + loc.getLatitude();
/*------- To get city name from coordinates -------- */
String cityName = null;
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
if (addresses.size() > 0) {


//Log.d("addresses", "----"+addresses);

//System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
currentLocality = cityName;
try {
currentLocality = addresses.get(0).getAddressLine(0) + "," + addresses.get(0).getSubLocality() + ", " + addresses.get(0).getLocality();

} catch (Exception e) {

e.printStackTrace();
}
}

currentLocation = loc.getLatitude() + "," + loc.getLongitude();
Log.d("current city", "printing"+currentLocality+";;;"+addresses.get(0).getAddressLine(0)+","+addresses.get(0).getSubLocality()+", "+addresses.get(0).getLocality());
Log.d("currentLocation", "printing"+currentLocation);

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.pratikbutani.workerexample.alram;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.pratikbutani.workerexample.R;
import com.pratikbutani.workerexample.databinding.ActivityMainBinding;

import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class MainActvity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE = 200;

private static final String TAG = "LocationUpdate";

ActivityMainBinding mainBinding;
private PendingIntent pendingIntent;
private AlarmManager manager;
boolean started;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

setSupportActionBar(mainBinding.toolbar);

if (!checkLocationPermission()) {
ActivityCompat.requestPermissions(this,
new String[]{ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}

try {
if (started==true) {
mainBinding.appCompatButtonStart.setText(getString(R.string.button_text_stop));
mainBinding.message.setText(getString(R.string.message_worker_running));
mainBinding.logs.setText(getString(R.string.log_for_running));

} else {
mainBinding.appCompatButtonStart.setText(getString(R.string.button_text_start));
mainBinding.message.setText(getString(R.string.message_worker_stopped));
mainBinding.logs.setText(getString(R.string.log_for_stopped));
}
} catch (Exception e) {
e.printStackTrace();
}

mainBinding.appCompatButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mainBinding.appCompatButtonStart.getText().toString().equalsIgnoreCase(getString(R.string.button_text_start))) {
// START Worker
started=true;
/* Retrieve a PendingIntent that will perform a broadcast */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);

Intent alarmIntent = new Intent(MainActvity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActvity.this, 0, alarmIntent, 0);
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 60000;// for 1 minute
// manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.d("printing pending intent", "--3-----"+pendingIntent);
try{
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
}catch(Exception e){
e.printStackTrace();
}

// Toast.makeText(MainActvity.this, "Location Worker Started : " + periodicWork.getId(), Toast.LENGTH_SHORT).show();

mainBinding.appCompatButtonStart.setText(getString(R.string.button_text_stop));
mainBinding.logs.setText(getString(R.string.log_for_running));
} else {
started=false;
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);

Toast.makeText(MainActvity.this, "Alarm Canceled", Toast.LENGTH_SHORT).show();

mainBinding.appCompatButtonStart.setText(getString(R.string.button_text_start));
mainBinding.message.setText(getString(R.string.message_worker_stopped));
mainBinding.logs.setText(getString(R.string.log_for_stopped));
}
}
});
}



/**
* All about permission
*/
private boolean checkLocationPermission() {
int result3 = ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION);
int result4 = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
return result3 == PackageManager.PERMISSION_GRANTED &&
result4 == PackageManager.PERMISSION_GRANTED;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0) {
boolean coarseLocation = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean fineLocation = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (coarseLocation && fineLocation)
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
}