安卓最佳数据存储和分享方式通过应用程序。

huangapple go评论66阅读模式
英文:

Android Best way to keep and share data through the app

问题

我正在制作一个小型Android应用程序,运行一个前台服务,该服务侦听并在新的短信消息到达时通知用户。服务启动时,它会使用动作android.provider.Telephony.SMS_RECEIVED在IntentFilter中注册BroadcastReceiver,并在停止时取消注册。该应用程序还会从内部存储中的JSON文件中获取用户数据以及一系列选定号码,以便接收器可以过滤出要通知用户的号码。我的MainActivity有3个按钮,启动和停止服务,以及一个“Setting”按钮,可转到SettingActivity,显示一些用户信息以及选定号码列表,并允许用户进行更改。我的问题是:共享整个应用程序的用户数据的最佳方法是什么,以便所有Activity、Service和Receiver都可以访问它?我已经考虑并尝试了一些方法:

  • 在MainActivity中从文件中获取用户数据,在应用程序启动时进行,然后通过Intent传递给其他组件:这在从一个Activity传递数据到另一个Activity以及Service时效果很好,但是对于BroadcastReceiver似乎不起作用,因为它注册为侦听SMS_RECEIVED而不是其他广播,即使我创建一个新的Intent并使用startBroadcast进行广播。
  • 使user对象静态:我尝试将User声明为公共静态,以便在MainActivity从文件中获取数据后,其他类可以调用它,基本上在MainActivity中放置一个public static User user,并在其他类中通过MainActivity.user来调用。这甚至在BroadcastReceiver中也能够正常工作,但我不确定这是否是共享数据的正确方式。
  • 最后一种方法是在每个调用每个Activity或Service时再次从文件中获取数据,这意味着每次调用每个Activity或Service时,都会从JSON文件中再次获取数据。我还没有尝试过,但我认为它可能会影响性能以及整个应用程序中的数据一致性。

那么,实现这一点的正确方法是什么呢?请给我一些建议。提前感谢您。

英文:

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.

答案1

得分: 1

有几些建议:

1 - 你可以将所需数据存储在共享首选项中,不过如果这是个人数据,例如密码、可识别个人信息,你可能不希望这样做。

2 - 类似于你在活动中所做的,你可以创建一个负责从文件获取用户数据并在应用程序被终止前保留数据的服务,例如 -

public class UserDataStore()

这样,你可以在启动时创建一个 UserDataStore 实例,然后通过依赖注入在整个应用程序中传递它。因此,无论在哪里创建处理广播事件的服务,你只需要将 UserDataStore 添加为参数,假设你首先创建了 UserDataStore。例如 -

public class BroadcastReceiverService(UserDataStore store)

这种方式还意味着你的 BroadcastReceiverService 更易于测试,因为你可以模拟 UserDataStore。

示例 -

public class MainActivity() {

    UserDataStore store = new UserDataStore()

    BroadcastReceiverService broadcastService = new BroadcastReceiverService(store)

}

然后在你的 UserDataStore 中 -

public class UserDataStore() {

    // 在这里获取关于用户的所有信息,如用户名、电子邮件等
    String name = "";

    public UserDataStore() {
        User user = getUserInfo();
        name = user.name;
    }

    public String getUserName() {
       return name;
    }  

}

最后,在你的广播接收器中:

public class BroadcastReceiverService(UserDataStore store) {

    public void receiveMessage(Message msg) {
        if (msg.username.equals(store.name)) // 继续处理
    }

}
英文:

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
    }

}

huangapple
  • 本文由 发表于 2020年10月16日 11:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64382515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定