我需要你的帮助。我是一名java开发人员,但还不是Android开发人员;)
我想开发一个具有以下功能的应用程序(4.0.3及更高版本):
- 申请:
+应用程序启动并提供一些设置
- 服务:
+我需要一个在Android启动时启动的后台服务
+该服务必须触发和检查新的日历事件
+服务检查事件,在某些情况下,它会打开一个弹出窗口
我做了一些教程并检查了一些android类。我的步骤和问题:
为了阅读日历活动,我会使用日历提供程序。 对于弹出窗口,我将创建一个AlertDialog,其中包含在服务中调用的自定义布局。
这可行吗?感谢您的回答和提示。
修改
感谢您的有用答案。经过一些测试后,我有一些跟进问题。
活动
public class MainActivity extends Activity {
// On the app the user can edit some setting
// But the app needn't be started that the service runs!!
// The service must be able to read the settings from this activity
}
BroadcastReceiver(用于启动)
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Start of the service (TriggerService)
}
}
}
AlarmManager服务
public class TriggerService extends IntentService {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
@Override
protected void onHandleIntent(Intent intent) {
// force invoke of CalendarChecker if we come from BootReceiver, then
// create the AlarmManager
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, CalendarChecker.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:00 a.m.
Calendar calendar = Calendar.getInstance();
...
// Set repeating interval
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
calendarChecker:
public class CalendarChecker {
// Checks the calendar events on the device and opens in some case a popup e.g.
...
CustomCalendarDialog dialog = new CustomCalendarDialog();
dialog.show(getFragmentManager(), "showPopup");
}
这是我的想法。但现在问题是:
- 在TriggerService中我需要活动的上下文 - 但我怎样才能得到它?
- 在CalendarChecker中,我需要活动的片段管理器 - 但我怎么能得到它呢?
感谢您的回答。
答案 0 :(得分:1)
使用ui为设置创建活动。您通常在哪里存储设置?手机上的区域设置还是有一些云解决方案?
在手机上,使用sharedpreference或sqlitedatabase进行设置
创建服务。我需要什么样的服务?
简单的服务,我建议使用START_STICKY
如何在启动时触发服务?
使用启动接收器
如何每天早上(当我没有启动时)触发服务? AlarmManager?
是警报管理员很好
如何在新的或修改过的日历活动上触发服务?
阅读此答案 https://stackoverflow.com/a/6175930/2923194
我可以在服务中调用弹出窗口吗?
创建pop uo活动并从服务
调用它答案 1 :(得分:0)
你的问题过于宽泛。只是提供一些简单的指示:
设置可以存储在SHARED PREFERENCES或SQLITE中。相关术语CONTENT PROVIDER
简单的REST网络服务就足够了。它将连接到您的服务器,以JSON的形式获取日历事件。 Consume the webservice response并解析JSON并在SQLITE中显示/保存以供日后检索。
通过启动,我假设你想要第一次启动应用程序时。您可以在共享首选项中使用标志变量,并根据标志值调用服务。
For checking if first day and calling webservice,您可以将日期保存为共享首选项中的值,如果日期更改,则假定它是用户首次登录当天。然后触发适当的Web服务。
通过HTTPclient处理事件并在需要时调用Web服务。
为了阅读日历活动,我会使用日历提供程序。对于弹出窗口,我将创建一个AlertDialog,其中包含在服务中调用的自定义布局。这是否可行?是!这非常可行!