gpt4 book ai didi

java - 广播接收者不响应广播消息

转载 作者:行者123 更新时间:2023-12-02 06:17:09 25 4
gpt4 key购买 nike

我设置了广播接收器,但它没有收到我发送的广播消息:

我的 list

<receiver android:name="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG" />
</intent-filter>
</receiver>

正在发送(调试显示我到达了发送线)

public class LocationService extends Service {
public static final String LOCATION_BROAD_MSG = "Hello";
private static final int TWO_MINUTES = 1000 * 60 * 2;

public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;

Intent intent;
int counter = 0;

@Override
public void onCreate() {
super.onCreate();
intent = new Intent(LOCATION_BROAD_MSG);
}

@Override
public void onStart(Intent intent, int startId) {

}

public class MyLocationListener implements LocationListener
{

public void onLocationChanged(final Location loc)
{
intent = new Intent(LOCATION_BROAD_MSG);
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
intent.putExtra("Latitude", loc.getLatitude());
intent.putExtra("Longitude", loc.getLongitude());
intent.putExtra("Provider", loc.getProvider());
sendBroadcast(intent);

}
}

}

正在接收(调试期间从未达到 onReceive)

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub


if (intent != null)
{

String action = intent.getAction();

if (action != null)
{
if (action.equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
// Log.d("receiver","action is: boot");
}

if (action.equalsIgnoreCase(LocationService.LOCATION_BROAD_MSG))
{
//Toast.makeText(, "wee hee", Toast.LENGTH_LONG);
}

}
}
}

}

最佳答案

更改此行:

public static final String LOCATION_BROAD_MSG = "com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG";

您已将com.example.manyexampleapp.LocationService.LOCATION_BROAD_MSG注册为接收器中的 Intent 过滤器。

关于java - 广播接收者不响应广播消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com