Boardcast03

触发器:       

Intent intent = new Intent();
intent.putExtra("names_music", list_names.get(positions));
intent.setAction("android.intent.action.names");
MyService.this.sendBroadcast(intent);

 

接收器:(单独的类中)

 class myReceiver extends BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String str=intent.getStringExtra("names_music");
}

}

 

注册: MyReceiver最好和发送端在同一个包中

<receiver android:name=".MyReciver">
<intent-filter >
<action android:name="android.intent.action.names"/>
</intent-filter>
</receiver>

 

接收器(在Activity中)

private BroadcastReceiver receiver=null;

receiver= new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String str=intent.getStringExtra("names_music");
player_names_textview.setText(str);
}

};

 

可以在onResume中注册接收器这样就不需要在manifest中配置了:

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.names");
this.registerReceiver(this.receiver, filter);

 

在activity退出时要取消注册

this.unregisterReceiver(this.receiver);

posted @ 2012-09-11 10:39  黑-色-柳—丁  阅读(407)  评论(0编辑  收藏  举报