简体   繁体   English

Android 通过应用程序保存和共享数据的最佳方式

[英]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.我正在制作一个运行前台服务的小型 Android 应用程序,该应用程序在收到新 SMS 消息时侦听并通知用户。 When the Service start it register the BroadcastReceiver with action android.provider.Telephony.SMS_RECEIVED in IntentFilter, and unregister it when stop.当服务启动时,它在 IntentFilter 中使用操作android.provider.Telephony.SMS_RECEIVED注册BroadcastReceiver ,并在停止时取消注册。 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.该应用程序还从内部存储的 JSON 文件中获取用户数据以及所选号码列表,以便接收器可以过滤出要通知用户的号码。 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.我的 MainActivity 有 3 个按钮,启动和停止服务,以及一个设置按钮,该按钮移动到 SettingActivity 显示一些用户信息以及所选数字的列表并允许用户更改它。 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从应用程序启动的 MainActivity 文件中获取用户数据并将其通过 Intent 传递给其他人:这在将数据从 Activity 传递到 Activity 以及 Service 时效果很好,但似乎不适用于BroadcastReceiver因为它已注册为侦听到SMS_RECEIVED而不是其他广播,即使我创建了一个新的 Intent 并用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使user对象静态化:我尝试将User设为公共静态,以便在 MainActivity 完成从文件中获取数据后可以被其他类调用,基本上将public static User user放在 MainActivity 中并由MainActivity.user调用它MainActivity.user其他班级的MainActivity.user ,这甚至可以与BroadcastReceiver一起使用,但我不确定这是否是在整个应用程序中共享数据的正确方法
  • 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.最后是在调用每个 Activity 或 Service 时从文件中获取数据,这意味着在调用每个 Activity 或 Service 时,它​​会再次从 JSON 文件中获取数据。 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. 1 - 您可以在共享首选项中存储您需要的数据,但如果这是个人数据,您希望保密(例如密码、个人身份信息),您可能不想这样做。

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 - 2 - 与您在活动中所做的类似,您可以创建一个服务,负责从文件中获取用户数据并保留它直到应用程序被终止,例如 -

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.这样你就可以在启动时创建一个 UserDataStore 的实例,然后通过依赖注入将它传递给你的应用程序。 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.因此,无论您在何处创建处理广播事件的服务,您都可以添加一个 UserDataStore 作为参数,假设您首先创建了 UserDataStore。 eg -例如——

public class BroadcastReceiverService(UserDataStore store)

This way also means that your BroadcastReceiverService is more testable as you can mock the UserDataStore.这种方式也意味着您的 BroadcastReceiverService 更具可测试性,因为您可以模拟 UserDataStore。

Example -例子 -

public class MainActivity() {

    UserDataStore store = new UserDataStore()

    BroadcastReceiverService broadcastService = new BroadcastReceiverService(store)

}

Then in your UserDataStore -然后在您的 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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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