Android equivalent to NSNotificationCenter

前端 未结 8 2063
旧时难觅i
旧时难觅i 2020-12-22 15:35

In the process of porting an iPhone application over to android, I am looking for the best way to communicate within the app. Intents seem to be the way to go, is this the b

8条回答
  •  失恋的感觉
    2020-12-22 16:10

    Here is something similar to @Shiki answer, but from the angle of iOS developers and Notification center.

    First create some kind of NotificationCenter service:

    public class NotificationCenter {
    
     public static void addObserver(Context context, NotificationType notification, BroadcastReceiver responseHandler) {
        LocalBroadcastManager.getInstance(context).registerReceiver(responseHandler, new IntentFilter(notification.name()));
     }
    
     public static void removeObserver(Context context, BroadcastReceiver responseHandler) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(responseHandler);
     }
    
     public static void postNotification(Context context, NotificationType notification, HashMap params) {
        Intent intent = new Intent(notification.name());
        // insert parameters if needed
        for(Map.Entry entry : params.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            intent.putExtra(key, value);
        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
     }
    }
    

    Then, you will also need some enum type to be secure of mistakes in coding with strings - (NotificationType):

    public enum NotificationType {
    
       LoginResponse;
       // Others
    
    }
    

    Here is usage(add/remove observers) for example in activities:

    public class LoginActivity extends AppCompatActivity{
    
        private BroadcastReceiver loginResponseReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
               // do what you need to do with parameters that you sent with notification
    
               //here is example how to get parameter "isSuccess" that is sent with notification
               Boolean result = Boolean.valueOf(intent.getStringExtra("isSuccess"));
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
            //subscribe to notifications listener in onCreate of activity
            NotificationCenter.addObserver(this, NotificationType.LoginResponse, loginResponseReceiver);
        }
    
        @Override
        protected void onDestroy() {
            // Don't forget to unsubscribe from notifications listener
            NotificationCenter.removeObserver(this, loginResponseReceiver);
            super.onDestroy();
        }
    }
    

    and here is finally how we post notification to NotificationCenter from some callback or rest service or whatever:

    public void loginService(final Context context, String username, String password) {
        //do some async work, or rest call etc.
        //...
    
        //on response, when we want to trigger and send notification that our job is finished
        HashMap params = new HashMap();          
        params.put("isSuccess", String.valueOf(false));
        NotificationCenter.postNotification(context, NotificationType.LoginResponse, params);
    }
    

    that's it, cheers!

提交回复
热议问题