android - 新的 Android 开发人员寻求构建应用程序的指导

标签 android

我是 Android 开发的新手,为了更加熟悉这个平台,我一直在阅读和研究。我的编程知识中等,我有 C++ 经验并且相当精通 Actionscript 3.0。我选择了主要的编程来从事游戏开发,但我的背景更具艺术性/设计性。

我对一个应用程序有一个相对简单的想法,我想从最实用的方式来实现它,这是我的第一次尝试。本质上,该应用程序的核心功能是绘制我将从星历表中获取的信息。在基于图表数据的一天中的某个时间,我想根据那天更改图像/显示信息。我正在努力寻找最好的方法来做到这一点。此外,我想为用户提供一个选项,让他们在发生更改时收到某种通知,但这是我在基本实现方面遇到的事情。

所以我的问题是,如果您要设置这样的东西,您会怎么做?我不确定的是设置一个时间元素,以便应用程序知道何时更改要显示的图像/数据。 如果你花时间阅读这篇文章,我真的很感激。 韦德。

最佳答案

这实际上取决于此图像的显示位置/时间/方式。我假设图像显示在正常 Activity 中:

研究使用 AlarmManager 设置警报。这些警报可以启动更改图像源的服务(可能更新图像资源或文件名的数据库条目)。在 Service 中完成工作后,发送一个 Broadcast(或 StickyBroadcast)并将结果放入 Bundle使用 Intent.putExtra()。此时您还可以使用 NotificationManager 设置状态栏通知。 NotificationPendingIntent 应该是显示图像的 Activity

查看 APIDemos 的“警报”和“通知”部分(/samples/android-8/ApiDemos/src/com/example/android/apis/app)

更新:Mark Murphy 的WakefulIntentService 发现了一个更强大的警报系统 here .

然后,在那个Activity中注册一个BroadcastReceiver,监听你在服务。这将监听服务何时完成。如果您在 Service 中使用 StickyBroadcast,数据将被缓存,当 Activity 到达堆栈的前端时数据可用。否则,您将需要保留数据(在 SQLite 数据库或 SharedPrefernces 中)并使用时间戳。

更新 - 演示持久数据存储:

开始学习 SQLite 的好地方是 Android Note Pad demo .这将向您介绍 Android 的许多关键方面,包括生命周期、ContentProviders(数据库包装器)和使用 Android Views(小部件)。

我提到的替代方法是使用应用程序的默认 SharedPreferences。一个简单的用法示例是这样的:

String imageUri = null;
long timestamp = 0;
final String currentImage = "current_image";
final String lastTimestamp = "last_timestamp"

// Access the default SharedPreferences
SharedPreferences preferences = 
    PreferenceManager.getDefaultSharedPreferences(this);
// The SharedPreferences editor - must use commit() to submit changes
SharedPreferences.Editor editor = preferences.edit();

// Get the current image URI
if (preferences.contains(currentImage) {
    imageUri = preferences.getString(currentImage, null);
}
// Get the last timestamp
if (preferences.contains(lastTimestamp) {
    timestamp = preferences.getLong(lastTimestamp, 0);
} 

// To set the SharedPreferences
editor.putString(currentImage, imageUri);
editor.putLong(lastTimestamp, timestamp);
editor.commit();

希望我理解正确,这对您有所帮助。

关于android - 新的 Android 开发人员寻求构建应用程序的指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3732487/

相关文章:

Android附加库项目?

android - 是否有适用于 Android 开发人员的 Python 绑定(bind)?

java - "list.clear()"上的 NullPointerException

java - 需要在 GridView 项目上点击两次

Android JSON 解析问题

java - 更改选定微调项的文本大小

android - 根据用户位置发送通知

Android:调用Web服务时操作超时

android - 使用 Android Studio update 2.2.0 更新 gradle 2.2 后无法创建 apk

android - Google Maps V2 setLocationSource 删除了蓝色的 MyLocation 图标