2014-11-24 162 views
0

我有三個活動
對活性產生我註冊的廣播接收器,然後我去活動B,從那裏我去活動C.
最後onBackPressed活動的C,我發送廣播 但onReceive不叫廣播接收器「接收」方法沒有被調用?

我的第一個活動

private MyBroadCastReceiver myRecevier = new MyBroadCastReceiver(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Submit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent frag=new Intent(MainActivity.this,Activity2.class); 
       //frag.putExtra("Limit", foo); 
       startActivity(frag); 
       // } 
      } 
     }); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     //Register the activity to the broadcast receiver 
     registerReceiver(myRecevier, new IntentFilter(MyBroadCastReceiver.ACTION)); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     //Unregister the activity from the broadcast receiver. Good practice ;) 
     unregisterReceiver(myRecevier); 
    } 

    public class MyBroadCastReceiver extends BroadcastReceiver{ 

     public static final String ACTION = "com.uberrueco.mybroadcastreceiver.receivers"; 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("MyBroadCastReceiver", "received"); 
      Toast.makeText(context,"Received "+intent.getStringExtra("editText"), Toast.LENGTH_LONG).show(); 

     } 
    } 
} 

次活動沒有任何關係,但打算向活動3

第三幕ivity

public class Activity3 extends Activity { 
    EditText etReceivedBroadcast; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity3); 
     etReceivedBroadcast = (EditText) findViewById(R.id.etReceivedBroadcast); 
    } 
    @Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 
     super.onBackPressed(); 
     Intent intent = new Intent(this, MyIntentService.class); 
     intent.putExtra("editText", etReceivedBroadcast.getText().toString()); 
     startService(intent); 
    } 
} 

最後我IntentService

public class MyIntentService extends IntentService{ 

    public MyIntentService(){ 
     super("MyIntentService"); 
    } 

    public MyIntentService(String name) { 
     super(name); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     Log.d("MyIntentService", "handling intent..."); 

     //Intent created for broadcasting 
     Intent intentBroadCast = new Intent(); 

     //Filter the broadcast to the action desired 
     intentBroadCast.setAction(MyBroadCastReceiver.ACTION); 
     intentBroadCast.putExtra("editText", intent.getStringExtra("editText")); 
     //Send the broadcast :D 
     sendBroadcast(intentBroadCast); 
    } 
} 
+0

你試過,你onHandleIntent()服務方法被調用的變化?得到日誌? – 2014-11-24 06:48:01

回答

3

您在MainActivity的onPause調用unregisterReceiver。所以你沒有收到廣播。

將註冊表移動到onCreate並取消註冊到您的MainActivity的onDestroy

+0

哦,我犯了這樣一個愚蠢的錯誤 謝謝@ nandeesh ...它工作正常 – 2014-11-24 10:30:23

0

如果你的onHandleIntent()被調用,那麼你應該嘗試像。

Intent intentBroadCast = new Intent(MyBroadCastReceiver.ACTION); 

    intentBroadCast.putExtra("editText", intent.getStringExtra("editText")); 
    //Send the broadcast :D 
    sendBroadcast(intentBroadCast); 
0

使像下面的代碼

private MyBroadCastReceiver myRecevier = new MyBroadCastReceiver(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Register the activity to the broadcast receiver 
    this.registerReceiver(myRecevier, new IntentFilter(MyBroadCastReceiver.ACTION)); 

    Submit.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent frag=new Intent(MainActivity.this,Activity2.class); 
      //frag.putExtra("Limit", foo); 
      startActivity(frag); 
      // } 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 


} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    //Unregister the activity from the broadcast receiver. Good practice ;) 
    this.unregisterReceiver(myRecevier); 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    //Unregister the activity from the broadcast receiver. Good practice ;) 
    unregisterReceiver(myRecevier); 
} 



public class MyBroadCastReceiver extends BroadcastReceiver{ 

    public static final String ACTION = "com.uberrueco.mybroadcastreceiver.receivers"; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("MyBroadCastReceiver", "received"); 
     Toast.makeText(context,"Received "+intent.getStringExtra("editText"), Toast.LENGTH_LONG).show(); 

    } 
} 
} 

ü需要使用這樣this.unregisterReceiver()this.registerReceiver()