BroadcastReceiver未被调用

时间:2012-09-23 11:31:10

标签: android broadcastreceiver

我有一些问题。我想传递信息。我使用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 ;

          }
    }

2 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

(这是用于连接)

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

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

答案 1 :(得分:0)

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