Android BroadcastReceiver 未触发
Android BroadcastReceiver not firing
此代码过去在旧 android 版本上运行良好。当我用最新的 Android Studio 更新它时,它不再有效。
我正在尝试将一个简单的字符串从我的 类 之一发送到主 activity。
我在清单中声明接收方...
<receiver android:name="com.domain.appname.MainActivity$MyReceiver">
<intent-filter>
<action android:name="MY_ACTION"/>
</intent-filter>
</receiver>
尝试使用 Intent 发送广播时。这是检测到位置已更改的时间
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){}
}
其中的 addStats 行仅用于调试,它们都会触发。即我看到它检测到新的 long/lat 的统计信息并且我收到 "after sendBroadcast" 消息,所以看起来该代码正在发送(或试图发送)广播。
现在在我的主activity我有这个作为接收者...
//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");
}
}
永远不会触发。我从未看到 "inside onReceive" 消息。
有什么我可能遗漏的想法吗?通过大量的 Whosebug 和 google 结果,以上似乎是我需要的才能使它正常工作。正如我所说,它过去工作正常(可能是 Android 的第 26 版),但我正在尝试在较新的 OS 版本上安装此应用程序 运行。
您可能需要使用明确的意图。参见 。
但是,如果您在同一个应用程序中进行广播,更好的选择是使用:
https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager#sendBroadcast(android.content.Intent)
此代码过去在旧 android 版本上运行良好。当我用最新的 Android Studio 更新它时,它不再有效。
我正在尝试将一个简单的字符串从我的 类 之一发送到主 activity。
我在清单中声明接收方...
<receiver android:name="com.domain.appname.MainActivity$MyReceiver">
<intent-filter>
<action android:name="MY_ACTION"/>
</intent-filter>
</receiver>
尝试使用 Intent 发送广播时。这是检测到位置已更改的时间
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){}
}
其中的 addStats 行仅用于调试,它们都会触发。即我看到它检测到新的 long/lat 的统计信息并且我收到 "after sendBroadcast" 消息,所以看起来该代码正在发送(或试图发送)广播。
现在在我的主activity我有这个作为接收者...
//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");
}
}
永远不会触发。我从未看到 "inside onReceive" 消息。
有什么我可能遗漏的想法吗?通过大量的 Whosebug 和 google 结果,以上似乎是我需要的才能使它正常工作。正如我所说,它过去工作正常(可能是 Android 的第 26 版),但我正在尝试在较新的 OS 版本上安装此应用程序 运行。
您可能需要使用明确的意图。参见
但是,如果您在同一个应用程序中进行广播,更好的选择是使用: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager#sendBroadcast(android.content.Intent)