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);