我有一些问题。我想转发消息。我使用 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 ;
}
}