- You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
- It is more efficient than sending a global broadcast through the system.
範例
LocalBroadcastManager mLocalBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); //要先getInstance
}
protected void onPause() {
mLocalBroadcastManager.unregisterReceiver(TTLNotifyReceiver);
}
protected void onResume() {
IntentFilter filter = new IntentFilter("com.test.hello");
mLocalBroadcastManager.registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//rece the Intent
}
};
沒有留言:
張貼留言