2012-09-23 143 views
0

我有一些問題。我想轉移消息。我用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 ; 

      } 
    } 

回答

1

你需要做的是這樣的:

(這是連接)

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

您需要註冊並設置意圖過濾器。

+0

registerReceiver(日誌,新的IntentFilter(「logGPS」));不工作onCreateView(片段) –

+0

http://stackoverflow.com/a/12242581/1568164 –

+0

檢出http://stackoverflow.com/questions/4134203/how-to-use-registerreceiver-method – opaque

0

有您註冊接收郵件?我在代碼中看不到這一點。

+0

不,沒有註冊,我不知道序列 - 這就是所有 –