Android 11 - LocationManager.addProximityAlert does not trigger alerts - java

The addProximityAlert call stopped triggering alerts on Android 11 devices.
I call the addProximityAlert for the special locations (calculated for the user location) from the Application.onCreate method or whenever the ACCESS_FINE_LOCATION permission is granted using the following code:
mLocationManager = (LocationManager) mApplicationContext.getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager != null) {
final Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = mLocationManager.getBestProvider(criteria, true);
if (!TextUtils.isEmpty(provider)) {
mLocationUpdatelistener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
subscribeForProximityAlert(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
final Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
if (lastKnownLocation == null) {
try {
mLocationManager.requestSingleUpdate(criteria, mLocationUpdatelistener, Looper.getMainLooper());
} catch (Throwable e) {
e.printStackTrace();
}
} else {
subscribeForProximityAlert(lastKnownLocation);
}
}
}
The subscribeForProximityAlert function is as follows (it is called actually after the successful single location update - I've checked that):
void subscribeForProximityAlert(Location location) {
String id;
double lat;
double lng;
long radius;
// ... compute the params above using the location
final Intent intent = new Intent(ACTION_FILTER);
intent.putExtra("_id", id);
intent.putExtra("_lat", lat);
intent.putExtra("_lng", lng);
intent.putExtra("_rad", radius);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(mApplicationContext, i, intent, 0);
mPendingIntents.add(pendingIntent);
mLocationManager.addProximityAlert(
lat,
lng,
(float) radius,
24 * 60 * 60 * 1000,
pendingIntent
);
}
The ACTION_FILTER is the com.example.appname.ProximityAlert exactly the value I use in manifest to subscribe for the Proximity Alert broadcasts (com.example.appname is the package name of the app):
<application>
<receiver android:name="com.example.appname.ProximityAlertBroadcastReceiver">
<intent-filter>
<action android:name="com.example.appname.ProximityAlert" />
</intent-filter>
</receiver>
</application>
The ProximityAlertBroadcastReceiver can be as simple as:
public class ProximityAlertBroadcastReceiver extends BroadcastReceiver {
public ProximityAlertBroadcastReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
}
}
its onReceive method is never called on Android 11 devices (checked with Lod.d and Toast.makeText...show() calls). Not when the app is running, not when it's in background. Not when I'm already in the radius of specified point, not when I'm entering/exiting.
Tried to add the android.permission.ACCESS_BACKGROUND_LOCATION permission to the manifest (nothing about it in the LocationManager.addProximityAlerts docs anyway) and grant the permission in system settings for the app, but it didn't help.
Also tried to reduce the targetSdkVersion from 30 to 28, but it didn't help.

The problem happened because of the implicit intent for the broadcast receiver, which is prohibited for the app targeting Android 8+ (SDK version 26):
https://developer.android.com/about/versions/oreo/background#broadcasts
After changing the
final Intent intent = new Intent(ACTION_FILTER);
to
final Intent intent = new Intent(mApplicationContext, ProximityAlertBroadcastReceiver.class);
I started to receive the broadcasts.
UPDATE: There's a bug in recent Android versions (11, 12, maybe others) which makes Geofences unresponsive on some devices and completely non-working on other devices if no app is actively requesting location at the moment:
https://issuetracker.google.com/issues/218335535
addProximityAlert creates a geofence under the hood, so it's affected too.

Related

Android 9 background location tracking using foreground service

I am using an android fused location client for background location tracking which will run even if the app is cleared from the memory. I am using foreground service for this. It is running perfectly for most of the devices except the Samsung Galaxy devices with power saver modes. I have also added the permission for ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS on app runtime but still, the Samsung devices do not track perfectly. I am also using geofencing intent service along with a partial wake lock. This app is working fine on all other devices. Please check the code that I am using for background locations. This is not the full code.
Also when I enable location toasts then the distance is tracking perfectly even on Samsung. But when toasts are disabled then it only works for the first time in Samsung.
/**********LocationUpdatesService class********/
public class LocationUpdatesService extends Service {
#Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
onNewLocation(locationResult.getLastLocation());
}
};
createLocationRequest();
if (checkPermissions()) {
getLastLocation();
}
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"myapp::MyWakelockTag");
wakeLock.acquire();
}
public void requestLocationUpdates() {
Log.i(TAG, "Requesting location updates");
//Utils.setRequestingLocationUpdates(this, true);
startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
} catch (SecurityException unlikely) {
//Utils.setRequestingLocationUpdates(this, false);
Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
}
}
public void removeLocationUpdates() {
Log.i(TAG, "Removing location updates");
removeActivityUpdatesButtonHandler();
try {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
wakeLock.release();
stopForeground(true);
stopSelf();
} catch (SecurityException unlikely) {
//Utils.setRequestingLocationUpdates(this, true);
Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service started");
boolean startedFromNotification = intent.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
false);
// We got here because the user decided to remove location updates from the notification.
if (startedFromNotification) {
//removeLocationUpdates();
//stopSelf();
}
startForeground(NOTIFICATION_ID, getNotification());
// Tells the system to not try to recreate the service after it has been killed.
return START_STICKY;
}
}
I am using this Bound service in my main activity.
The above is just a sample code for anyone's reference. Please let me know if anyone knows the solution. I am just having trouble in the case of Samsung devices with a power saver. Otherwise, my service is working fine and tracking the distance perfectly.

GEOFENCE_NOT_AVAIBLE (code 1000) while trying to set up geofence

The problem occurs on Android older than Oreo and both Oreo and newer.
I can't get geofences working even though following steps are done:
Location services are set to High Accuracy
Wi-Fi and mobile data are enabled
Application is granted location permissions
Google Services are added to the project
Google Services and Play Store are up to date and installed on the device
Disabled battery optimizations (testing purpose)
I've checked with the following code if GPS_PROVIDER and NETWORK_PROVIDER are enabled:
#Override
protected void onResume() {
super.onResume();
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("Provider", "Provider is not avaible");
} else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.v("Provider", "GPS Provider is avaible");
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("Network Provider", "Provider is not avaible");
} else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.v("Network Provider", "provider is avaible");
}
}
Those both above gave me positive result, so problem can't be here.
Exact error:
E/Geofence: com.google.android.gms.common.api.ApiException: 1000:
I set mGeofencingClient in the begin of onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGeofencingClient = LocationServices.getGeofencingClient(getApplicationContext());
I set geofences with the following code:
mGeofenceList.add(
new Geofence.Builder()
.setRequestId("blablabla")
.setCircularRegion(50.32, 43.23, 232)
.setExpirationDuration(-1L)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
// }
PermissionCheck mPermissionCheck = new PermissionCheck();
if (!mPermissionCheck.isPermissionGranted(getApplicationContext())){
mPermissionCheck.askForPermission(MainActivity.this);
return;
}
setGeofences();
}
private GeofencingRequest getGeofencingRequest(){
if (mGeofenceList.isEmpty()){
return null;}
Log.v("mGeofenceList", mGeofenceList.toString());
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
GeofencingRequest.INITIAL_TRIGGER_EXIT);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent(){
if (mGeofencePendingIntent != null){
return mGeofencePendingIntent;
}
Intent intent = new Intent(getApplicationContext(), Geofencing.class);
mGeofencePendingIntent = PendingIntent.getService(getApplication(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
#SuppressLint("MissingPermission")
private void setGeofences(){
GeofencingRequest geofencingRequest = getGeofencingRequest();
PendingIntent pi = getGeofencePendingIntent();
mGeofencingClient.addGeofences(geofencingRequest, pi)
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("Geofences", "geofencing set up succesfully");
Toast.makeText(MainActivity.this, "Geofences set up", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(MainActivity.this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e("Geofence", e.toString());
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("Provider", "Provider is not avaible");
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("Network Provider", "Provider is not avaible");
}
}
});
}
This code is almost the same as from Google Documentation.
Manifest permission:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.location.network"/>
<uses-feature android:name="android.hardware.location.gps"/>
Gradle:
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
Can anyone see the mystake I could have done?
Thanks in advance!
OK this is a minimal working program for geofences based on you OP - just to rule out your code implementation - there's a couple other interfaces implemented for other tests so ignore.
"Working" means it successfuly adds the geofence.:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
private List<Geofence> mGeofenceList = new ArrayList<>();
private GeofencingClient gfc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
gfc = LocationServices.getGeofencingClient(getApplicationContext());
mGeofenceList.add(new Geofence.Builder().setRequestId("aa").setCircularRegion(50.32, 43.23, 232).setExpirationDuration(-1L).setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT).build());
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
}
else {
setGeofences();
}
}
private GeofencingRequest getGeofencingRequest(){
if (mGeofenceList.isEmpty()){
return null;}
Log.v("mGeofenceList", mGeofenceList.toString());
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
GeofencingRequest.INITIAL_TRIGGER_EXIT);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent mGeofencePendingIntent;
private PendingIntent getGeofencePendingIntent(){
if (mGeofencePendingIntent != null){
return mGeofencePendingIntent;
}
Intent intent = new Intent(getApplicationContext(), Object.class);
mGeofencePendingIntent = PendingIntent.getService(getApplication(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
#SuppressLint("MissingPermission")
private void setGeofences(){
GeofencingRequest geofencingRequest = getGeofencingRequest();
PendingIntent pi = getGeofencePendingIntent();
gfc.addGeofences(geofencingRequest, pi)
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("Geofences", "geofencing set up succesfully");
Toast.makeText(MapsActivity.this, "Geofences set up", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(MapsActivity.this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e("Geofence", e.toString());
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("Provider", "Provider is not avaible");
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("Network Provider", "Provider is not avaible");
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
setGeofences();
}
}
After some investigation I found I could recreate the 1000 error code with this code sample. It is based on this forum post: https://androidforums.com/threads/error-adding-geofence-on-android-8.1289302/
So to follow those directions (to fix - but I flipped them to recreate and then fix):
Use phone "Settings | Security & location | Location | Mode" - toggle between "High accuracy, Battery saving or Device only" until you get this prompt (the settings path will vary depending on android build):
In this example code - if you respond with "DISAGREE", the example code will generate the 1000 error code; if you repeat and respond with "AGREE" it will be successful in adding the geofence.
for android oreo to android S make sure to access setting high priority because geofence need that or it became error 1000
fun ceksetting(){
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
val locationRequest = LocationRequest()
locationRequest!!.interval = 50000
locationRequest!!.fastestInterval = 50000
locationRequest!!.smallestDisplacement = 170f // 170 m = 0.1 mile
locationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function
val client: SettingsClient = LocationServices.getSettingsClient(requireActivity())
val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
task.addOnSuccessListener { locationSettingsResponse ->
//here call your geofence
}
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException){
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
exception.startResolutionForResult(requireActivity(),
REQUEST_CHECK_SETTINGS)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}

Not able to get location with Service (LocationManager)

i am in real trouble here. Trying to get lat and lon with a background service every 3 sec but i am only able to get some data written when i click send lat and lon in extended controls of the emulator , so both phone and emulator are not working. Here is my code below, it would be awesome if someone could help me. Thanks!
Service
public class GPSService extends Service {
private static final String TAG = "GpsService";
private LocationListener locationListener;
private LocationManager locationManager;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
i.putExtra("latExtra",location.getLatitude());
i.putExtra("lonExtra",location.getLongitude());
sendBroadcast(i);
Log.i(TAG, "onLocationChanged: extras lat lon"+location.getLatitude()+" "+location.getLongitude());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
Log.i(TAG, "onProviderDisabled: DISABLED");
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager =(LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
String provider =locationManager.getBestProvider(c,true);
Log.i(TAG, "onCreate: bestProvider "+provider);
//noinspection MissingPermission
locationManager.requestLocationUpdates(provider,2000,0,locationListener);
}
#Override
public void onDestroy() {
super.onDestroy();
if (locationManager != null){
Log.i(TAG, "onDestroy: Location manager nije null i brisem");
//noinspection MissingPermission
locationManager.removeUpdates(locationListener);
}
}
}
MainActivity
private final String TAG = "Main";
...
BroadcastReceiver broadcastReciever;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//setStatusBarTranslucent(false);
if(!runtimePermisions()){
startLocationUpdate();}
...
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//stopService();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//REQUEST PERMISSION
Log.i(TAG, "onClick: NO PERMISION");
} else {
Log.i(TAG, "onClick: got permision");
}
...
}
public void startLocationUpdate(){
Intent i = new Intent(this,GPSService.class);
startService(i);
Log.i(TAG, "startLocationUpdate: Pokrenuo sam service");
}
#Override
protected void onResume() {
super.onResume();
if (broadcastReciever == null){
broadcastReciever = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
lat = (Double) intent.getExtras().get("latExtra");
lon = (Double) intent.getExtras().get("lonExtra");
Log.i(TAG, "onReceive: lat lon "+lat+" "+lon);
}
};
}
registerReceiver(broadcastReciever,new IntentFilter("location_update"));
}
#Override
protected void onDestroy() {
super.onDestroy();
if (broadcastReciever!=null){
unregisterReceiver(broadcastReciever);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults [0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
startLocationUpdate();
}else{
runtimePermisions();}
}
}
private boolean runtimePermisions() {
if (Build.VERSION.SDK_INT >= 23 &&ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
},100);
return true;
}
return false;
}
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.digiart.yoweather">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:theme="#style/SettingsTheme"></activity>
<service android:name=".Gps.GPSService"/>
</application>
</manifest>
Again... Any help would be great! THANKS :D
As K. Sopheak said, it can take a while to get the location. From the documentation:
It may take a while to receive the first location update. If an immediate location is required, applications may use the getLastKnownLocation(String) method.
So, you could try getting the last known location using getLastKnownLocation(String) when the service is started and, assuming it exists, broadcast it in the same way you would a location update. Bear in mind, though, that the last known location could be out of date. Depending on what you're using the location for this may or may not be acceptable.
Also, as an aside, a couple of thoughts:
You said 3 seconds, but the code uses 2,000 milliseconds - was that just a typo?
The frequency of location updates is a minimum time - you are not guaranteed to get updates that often. As per the documentation:
The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications.
Is there any particular reason you need location updates at such a high frequency? Obtaining a location can be battery intensive, particularly given that you are requesting FINE as well as COARSE location permissions, so requesting it so frequently could place an enormous drain on device battery life. This is particularly so given that the code is running in a service and will therefore continue to run even when the application is in the background or the user is in an activity which does not require location data. Again, from the documentation:
Choosing a sensible value for minTime is important to conserve battery life. Each location update requires power from GPS, WIFI, Cell and other radios. Select a minTime value as high as possible while still providing a reasonable user experience. If your application is not in the foreground and showing location to the user then your application should avoid using an active provider (such as NETWORK_PROVIDER or GPS_PROVIDER), but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes) or greater. If your application is in the foreground and showing location to the user then it is appropriate to select a faster update interval.
Google recommends using the Google Play services location APIs instead of the Android framework location APIs:
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.

Android Studio: App crashes on some phones

I'm new at android development and really confused by this problem.
I'm making a simple app which has 3 TextViews: text, text2 and text3.
text shows the latitude of user
text2 shows the longitude of the user and
text3 shows distance between user and the place called "Azadi Square".
The app runs properly on Samsung Galaxy S4 and Samsung Galaxy Tab S, but in Huawei Y511 and one of Sony Xperias (I do not know the exact name.), the app doesn't Open and says The App has unfortunately stopped
Here is my Java code:
public class Map extends Activity implements LocationListener {
private TextView text,text2,text3;
private String provider;
private LocationManager locationManager;
private double latitude,longitude;
private Location azadiSquare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
text = (TextView)findViewById(R.id.text);
text2 = (TextView)findViewById(R.id.text2);
text3 = (TextView)findViewById(R.id.text3);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria,false);
azadiSquare = new Location(provider);
azadiSquare.setLatitude(35.6996540);
azadiSquare.setLongitude(51.3379906);
Location location = locationManager.getLastKnownLocation(provider);
text.setText(location.getLatitude()+"");
text2.setText(location.getLongitude()+"");
text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider,400,1,this);
}
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
text.setText(latitude+"");
text2.setText(longitude+"");
text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String s) {
Toast.makeText(this,"Enabled new provider " + provider,Toast.LENGTH_LONG).show();
}
#Override
public void onProviderDisabled(String s) {
Toast.makeText(this,"Disabled provider " + provider,Toast.LENGTH_LONG).show();
}
private double getDistanceFromLatLonInKm(double lat1,double lon1,double lat2,double lon2) {
double R = 6371; // Radius of the earth in km
double dLat = deg2rad(lat2-lat1); // deg2rad below
double dLon = deg2rad(lon2-lon1);
double a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c; // Distance in km
return d;
}
private double deg2rad(double deg) {
return deg * (Math.PI/180);
}
private int distanceBetweenMeter(Location location1, Location location2){
double distanceKM = getDistanceFromLatLonInKm(location1.getLatitude(),location1.getLongitude(),
location2.getLatitude(),location2.getLongitude());
int distanceMeter = (int)(distanceKM*1000);
return distanceMeter;
}
}
Note: I just deleted the imports!
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Map" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Notes: I turned On the Location of the phone before opening the app, so it is not the problem. The other thing I want to say is that all the calculations are okay and they are working fine.
On Android 6.0 (API level 23) and higher you need to request permission for ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION at run time. See, Requesting Permissions at Run Time. That may be the case.
The best thing to do is to logcat the issue and backtrace it so you could see where it's going bad or at least have one clue. If the answer from user35603 didn't worked then try this and post the logcat part regarding to your issue.
Finally the problem is solved! This part of the code was making trouble:
Location location = locationManager.getLastKnownLocation(provider);
text.setText(location.getLatitude()+"");
text2.setText(location.getLongitude()+"");
I guess it takes a little bit of time for location to get the current location. Actually, when app wants to run the line below, location is still null and app crashes:
text1.setText(location.getLatitude()+"");
I solved the crash with something like this:
Location location = locationManager.getLastKnownLocation(provider);
while(location==null){}
text.setText(location.getLatitude()+"");
text2.setText(location.getLongitude() + "");
text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
Crash is solved, but the app freezes with white screen. I don't know exactly, but I guess it is trapped on while loop. Thanks!
public class MainActivity extends AppCompatActivity {
pb=findViewById(R.id.progressbar);
Example ex=new Example();//The subclass object is created here
boolean stop=false;//a boolean variable to start or stop the thread to get location.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (location== null) {
showSettingDialog();//this will show your custom way of displaying
//the location setting intent
new Thread(ex).start();//here the fetching location gets started
}
else{
//here write code after your location is fetched
//here to got some other base activity or stay in this
finish();//if you wan to stay in this activity then omit this line.
}
}
public class Example implements Runnable{ //This is a sub class in your main activity.
#Override
public void run() {//overriding run medthod
try {
while(location==null) { //this part keeps on running whithout
//ui freeze
if (stop) {
return;
}
runOnUiThread(new Runnable() {
#Override
public void run() {
pb.setVisibility(View.VISIBLE);
}
});
getLastLocation();//this function will fetch location.
Thread.sleep(1500);
pb.setVisibility(View.INVISIBLE);
if (location != null) {
//do your code here
stop = true;
finish();
}
}
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}

Service containing BroadCastReceiver not functioning correctly

Please see edits before answering!
I have an app which contains a BackgroundService class:
public class BackgroundService extends Service {
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction("com.spotify.music.playbackstatechanged");
filter.addAction("com.spotify.music.metadatachanged");
filter.addAction("com.spotify.music.queuechanged");
registerReceiver(receiver, filter);
Log.e("Playing:", "APP IS PLAYING");
Notification notification = new Notification();
startForeground(1, notification);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long timeSentInMs = intent.getLongExtra("timeSent", 0L);
String action = intent.getAction();
if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
String trackId = intent.getStringExtra("id");
String artistName = intent.getStringExtra("artist");
String albumName = intent.getStringExtra("album");
String trackName = intent.getStringExtra("track");
int trackLengthInSec = intent.getIntExtra("length", 0);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
Log.e("Playing:","TRUE");
}
}
};
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
static final class BroadcastTypes {
static final String SPOTIFY_PACKAGE = "com.spotify.music";
static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
}
and this is declared in my manifest:
<service
android:name=".BackgroundService"
android:enabled="true" >
<intent-filter>
<action android:name="com.spotify.music.playbackstatechanged" />
<action android:name="com.spotify.music.metadatachanged" />
<action android:name="com.spotify.music.queuechanged" />
</intent-filter>
</service>
So essentially my objective is to have my BackgroundService initialized when my app is opened, and to have it continue to run in the Background doing whatever I need it to do. As of now, I am using logs to determine whether my "setup" is working, but when I run my app, I am unable to see an logs even after I tested all actions that should have triggered my BroadCastReceiver. Furthermore, my persistent notification should have changed had my service been running, but it does not...
Edit::
So, I added logs to my BackgroundService's onCreate() and onReceive() methods, however, neither seem to be appearing. Im wondering, do I need to do something in my launcher activity to initialize the service? Furthermore, no notification is shown so I assume the Service is not being started for some reason...
Latest Edit:
So I added the following code to my Main activity to see if it would make a difference:
startService(new Intent(this,BackgroundService.class));
And after debugging my app, I began to see the following error:
java.lang.RuntimeException: Unable to create service com.aurum.mutify.BackgroundService: java.lang.SecurityException: Isolated process not allowed to call registerReceiver
pointing to my BroadCast Receiver class.
Intent services are designed for short tasks. And your intent handling method is empty.
If you need long running task in the background use standard service and call start foreground. This will minimize chance of system destroying your service.
To learn more go here
EDIT
Try overriding onStartCommand method. this method is called when service is started and usually you do all stuff here. Remember that there are 3 options to return.
Edit 2:
try something like this
in on create
PendingIntent pi;
BroadcastReceiver br;
Intent myIntent;
#Override
public void onCreate()
{
super.onCreate();
myIntent = new Intent("something")
if(Build.Version.SDK_INT >= 16) //The flag we used here was only added at API 16
myIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
//use myIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); if you want to add more than one flag to this intent;
pi = PendingIntent.getBroadcast(context, 1, myIntent, 0);
br = new BroadcastReceiver ()
{
public void onReceive (Context context, Intent i) {
new thread(new Runnable()
{
public void run()
{
//do something
}
}).start();
}
};
And then in on start command
this.registerReceiver(br, new IntentFilter("something"));

Categories

Resources