[英]Android BroadcastReceiver not firing
This code used to work fine on older android versions. 这段代码过去在较旧的android版本上运行良好。 When I updated it with latest Android Studio it no longer works.
当我使用最新的Android Studio更新它时,它不再起作用。
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. 其中的addStats行仅用于调试,它们均会触发。 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.
也就是说,我看到它检测到新的长/短的统计信息,并且收到“ after sendBroadcast”消息,因此看起来该代码正在发送(或尝试发送)广播。
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.
我从没看到“ inside onReceive”消息。
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.
经过大量的stackoverflow和google结果,以上似乎是我需要使它工作的原因。 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.
正如我所说的那样,它过去运行正常(也许是Android的26版本),但我正在尝试使该应用程序在更新的OS版本上运行。
You probably need to use an explicit intent. 您可能需要使用明确的意图。 See https://stackoverflow.com/a/49197540/383676 .
参见https://stackoverflow.com/a/49197540/383676 。
However, if you're broadcasting within the same app a better option would be to use: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager#sendBroadcast(android.content.Intent) 但是,如果要在同一应用程序中广播,则更好的选择是使用: https : //developer.android.com/reference/android/support/v4/content/LocalBroadcastManager#sendBroadcast(android.content.Intent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.