User Tools

Site Tools


code:android-code:send-receive-notify

如何发送和接收notify

在Android中有很完整的消息通知机制,可以很方便的发送一个通知,然后注册一个receiver去接收这些消息.

发送notify

  public static void notifyAuthenticationChange(Context ctx, String authority, boolean authenticated) {
        Utils.log("notifyAuthenticationChange,authority="+authority);
    	Intent broadcast = new Intent(ACTION_SOCIAL_AUTH);
        broadcast.putExtra(EXTRA_AUTHORITY, authority);
        broadcast.putExtra(EXTRA_AUTHENTICATED, authenticated);
        ctx.sendBroadcast(broadcast);
    }

接收notify

	private void registerAuthReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(AuthObserver.ACTION_SOCIAL_AUTH);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		registerReceiver(mAuthChangeReceiver, filter);
	}
 
	private BroadcastReceiver mAuthChangeReceiver = new BroadcastReceiver() {
 
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Utils.log("onReceive,the Auth may changed.intent=" + intent);
		}
	};
如果不用的时候还需要unregister:

unregisterReceiver(mAuthChangeReceiver);
/var/www/dokuwiki/wiki/data/pages/code/android-code/send-receive-notify.txt · Last modified: 2016/05/05 13:07 by 127.0.0.1