This code used to work fine on older android versions. When I updated it with latest Android Studio it no longer works.
I am trying to send a simple string from one of my classes to the main activity.
I declare the receiver in my manifest...
<receiver android:name="com.domain.appname.MainActivity$MyReceiver">
<intent-filter>
<action android:name="MY_ACTION"/>
</intent-filter>
</receiver>
When trying to send the broadcast using an intent. This is when the location is detected as changed
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
addStats("onLocationChanged - "+mLastLocation.getLatitude()+" "+mLastLocation.getLongitude()+" "+mLastLocation.getAccuracy()+" "+mLastLocation.getSpeed());
mLastLocation.set(location);
//send latitude, longitude and accuracy as broadcast to main activity
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", mLastLocation.getLatitude()+" "+mLastLocation.getLongitude()+" "+mLastLocation.getAccuracy()+" "+mLastLocation.getSpeed());
sendBroadcast(intent);
addStats("after sendBroadcast");
}
@Override
public void onProviderDisabled(String provider){}
@Override
public void onProviderEnabled(String provider){}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){}
}
The addStats lines in there are just for debugging and they all fire. ie I see the stats for it detecting new long/lat and I get the "after sendBroadcast" message, so it looks like that code is sending (or attempting to send) the broadcast.
Now in my main activity I have this as the receiver...
//receives the broadcasts from the MyLocationService service
public static class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
addStats("inside onReceive");
String datapassed = arg1.getStringExtra("DATAPASSED");
}
}
That never gets triggered. I never see the "inside onReceive" message.
Any ideas what I may be missing? Been through a lot of stackoverflow and google results and the above seems to be what I need to get this working. As I said it used to work fine (maybe version 26 of Android), but I am trying to get this app running on newer OS versions.