简体   繁体   中英

Android Best way to keep and share data through the app

I am making a small Android app that run a Foreground Service which listen and notify the user whenever a new SMS message arrived. When the Service start it register the BroadcastReceiver with action android.provider.Telephony.SMS_RECEIVED in IntentFilter, and unregister it when stop. The application also get the user data along with a list of selected number from a JSON file in internal storage so that the Receiver can filter out which number to notify the user. My MainActivity has 3 buttons, Start and Stop Service, along with a Setting button that move to SettingActivity that display some user info as well as the list of selected number and allow the user to change it. My question is that: What is the best way to share the user data throughout the app so that all Activity as well as Service and Receiver can access it? I have thought of and tried a few ways:

  • Getting the user data from file in MainActivity where the application start up and pass it through Intent to others: this worked well when passing data from Activity to Activity as well as Service, but does not seem to work with BroadcastReceiver since it is registered to listen to SMS_RECEIVED and not other Broadcast even if I create a new Intent and broadcast it with startBroadcast
  • Making the user object static: I tried with making the User public static so that it can be called by other class when the MainActivity is done getting the data from the file, basically putting a public static User user in MainActivity and calling it by MainActivity.user in other class, this worked even with BroadcastReceiver but I am not sure if this the right way to share data throughout the application
  • Lastly is to get the data from the file when each Activity or Service is called, this mean when each Activity or Service is called, it get data again from the JSON file. I have not tried it yet but I think it might impact the performance as well as data consistency throughout the app.

So what is the right way to implement this? Please give me some suggestion. Thank you guys in advance.

There are a few suggestions for this:

1 - You could store the data you need in shared preferences, though if this is personal data, that you'd want to keep private (eg passwords, personally identifiable information) you probably don't want to do this.

2 - Similarly to what you are doing in your activity, you could create a service which is responsible for getting the user data from the file and keeps hold of it until the app is killed, for example -

public class UserDataStore()

This way you can create an instance of a UserDataStore on startup and then, through dependency injection, pass it around your app. So wherever you create your service that handles the broadcast events, you can just add a UserDataStore as a parameter, assuming you create your UserDataStore first. eg -

public class BroadcastReceiverService(UserDataStore store)

This way also means that your BroadcastReceiverService is more testable as you can mock the UserDataStore.

Example -

public class MainActivity() {

    UserDataStore store = new UserDataStore()

    BroadcastReceiverService broadcastService = new BroadcastReceiverService(store)

}

Then in your UserDataStore -

public class UserDataStore() {

// in here you want to get all your info about the user eg username, email etc

    String name = ""

    init {
        User user = getUserInfo()
        name = user.name
    }


    public String getUserName() {
       return name
    }  

}

Then finally in your broadcast receiver

public class BroadcastReceiverService(UserDataStore store) {

    public void receiveMessage(Message msg) {
        if(msg.username == store.name) // continue
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM