在IntentService里发消息,调试能够进去
Intent intent_pop = new Intent("com.wei.geofencing.GeofenceIntentService");
intent_pop.putExtra("msg", "Entered");
LocalBroadcastManager.getInstance(this.getApplicationContext()).sendBroadcast(intent);
在Fragment里直接写了个BroadcastReceiver,不用在配置文件里配置了。但是onReceive方法死活不调用
public class Tab_Results extends Fragment {
private final BroadcastReceiver mEnterZoneReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals("com.wei.geofencing.GeofenceIntentService")){
System.out.println("OK");
Toast.makeText(getActivity(), "Enter Zone", Toast.LENGTH_SHORT).show();
}
}
};
@Override
public void onStart() {
super.onStart();
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.wei.geofencing.GeofenceIntentService");
broadcastManager.registerReceiver(mEnterZoneReceiver, intentFilter);
}
@Override
public void onStop() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mEnterZoneReceiver);
super.onStop();
}
}
是不是Service和Fragment要放在同一个包,或不同的包都不行。
在Service里发消息用this.getApplicationContext()这个上下文,收消息用的fragment里的getActivity(),这样没问题吧。
还有人说是service要配置到主进程里,比如下面这样,也不行。
<service
android:name="com.media.ffmpeg.ffmpegService"
android:enabled="true"
android:process="com.media.ffmpegservice">
</service>
------解决思路----------------------
我想说,可能是安卓已经抛弃了这个本地广播。我也试过了,不调用,但换成context直接注册就调用。
------解决思路----------------------
broadcastManager.registerReceiver(mEnterZoneReceiver,?intentFilter);??? 直接写成 context..registerReceiver(mEnterZoneReceiver,?intentFilter);?? ,这个context就是你的getActivity();