我有一些問題。我想轉移消息。我用context.sendBroadcast發送和廣播接收器 - 接收消息BroadcastReceiver未被調用
public class GPS_module implements LocationListener {
private Context context;
public GPS_module(Context ctx) {
context = ctx;
manager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
}
.....
public void sendMessage(String str) {
Intent intent = new Intent("logGPS");
intent.putExtra("Message",str);
context.sendBroadcast(intent);
}
}
用於接收我用下面的源代碼
public class Fragment_1 extends Fragment{
.......
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.right_panel_1, null);
BroadcastReceiver log = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("logGPS"))
{
myLog(intent.getStringExtra("Message"));
}
}
};
return myFragmentView ;
}
}
registerReceiver(日誌,新的IntentFilter(「logGPS」));不工作onCreateView(片段) –
http://stackoverflow.com/a/12242581/1568164 –
檢出http://stackoverflow.com/questions/4134203/how-to-use-registerreceiver-method – opaque