Broadcast 廣播機制
//傳送廣播
public class SentBroadcas extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.Button01);
//點擊Button後發送廣播
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent().setAction("android.intent.test")
sendBroadcast(intent);
}
});
}
}
//接收廣播
public class BroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.test")){
}
}
}
//在AndroidManifest.xml註冊
<receiver android:name="BroadcastReciever">
<intent -filter="">
<action android:name="android.intent.test">
</action></intent>
</receiver>
----------------------------------------------------------------------------------------------
沒有留言:
張貼留言