1

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

          }
    }
4

2 回答 2

2

您需要执行以下操作:

(这是为了连接)

   registerReceiver(myReceiver,
           new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

您需要注册并设置一个意图过滤器。

于 2012-09-23T11:57:17.027 回答
0

你注册你的接收器了吗?我在您的代码中没有看到这一点。

于 2012-09-23T11:47:40.893 回答