在我的应用程序我有一个表,在毫秒日期行SQLite数据库。我想表明的通知天天如果 30天内,因为存储在我的数据库中的最后一个日期值传递。服务似乎是实现这一目标的检查,一个好办法。
In my app I have SQLite database that has one table with date rows in milliseconds. I would like to have a notification shown every day IF 30 days has passed since the last date value stored in my database. A service seems to be a good way to accomplish this check up.
我跑进Commonsware的 WakefulIntentService ,并认为这可能是答案,但我真的不知道我应该怎么实现它。在演示它开始5分钟后一种服务,因为启动完成这仅仅是罚款,但做什么我需要添加得到它也开始在每个中午。 (...但只显示一个通知/天,而不是两个,从引导和每天定时清理)
I ran into Commonsware's WakefulIntentService and thought it could be the answer but I really don't know how should I implement it. In the demo it starts a service after 5 minutes since boot is complete which is just fine but what do I need to add to get it also start at every noon. (... but only to show one notification / day, not both, as from boot and regular daily check up)
我知道这可能使用AlarmManager得到解决,但真的不知道怎么办。因此,帮助我需要的是给我一些样本/关键点,以获得在每次启动和/或每天的服务启动没有应用程序的运行。
I know this could be solved using AlarmManager but really don't know how. So, the help I need is to give me some samples / key points to get the service start on every boot and/or every day without app running.
感谢
推荐答案Android的alarmmanager是你的答案。将其与广播接收器也将复位报警的电话叫醒。
Android alarmmanager is your answer. use it with a broadcast receiver which also resets the alarms on phone wake.
现在与code例如: 一个方法内设置报警:
Now with code example: Setting alarm inside a method:
Intent intent = new Intent(context, AlarmReceiver.class); intent.setAction("packagename.ACTION"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.cancel(pendingIntent); alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
接收器为你的时间:
Receiver for your interval:
public class AlarmReceiver extends BroadcastReceiver { private final String SOMEACTION = "packagename.ACTION"; //packagename is com.whatever.www @Override public void onReceive(Context context, Intent intent) { Time now = new Time(); now.setToNow(); String time = FileHandler.timeFormat(now); String action = intent.getAction(); if(SOMEACTION.equals(action)) { // here you call a service etc. }
接收器复位报警时手机已经关机。
Receiver for resetting alarms whenever phone has been shut down.
public class AlarmSetter extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // get preferences SharedPreferences preferences = context.getSharedPreferences("name_of_your_pref", 0); Map<String, ?> scheduleData = preferences.getAll(); // set the schedule time if(scheduleData.containsKey("fromHour") && scheduleData.containsKey("toHour")) { int fromHour = (Integer) scheduleData.get("fromHour"); int fromMinute = (Integer) scheduleData.get("fromMinute"); int toHour = (Integer) scheduleData.get("toHour"); int toMinute = (Integer) scheduleData.get("toMinute"); //Do some action } } }
清单非常重要,这是在应用程序中加入:
Manifest very important, this is added under application:
<receiver android:name="AlarmReceiver"> <intent-filter> <action android:name="packagename.ACTION"/> <action android:name="packagename.ACTION2"/> </intent-filter> </receiver> <receiver android:name="AlarmSetter" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
另外为了这个工作,你需要添加允许收取开机广播在清单中有如下一行:
Also in order for this to work you need to add permission to receive the boot Broadcast in the manifest with following line:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
希望这会茅塞顿开,如有错误,PLZ告诉。
Hope this cleared things up, if any errors plz tell.
修改(添加alarmsetter为例):
Edit (added alarmsetter example):
public class AlarmSetter extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Do your stuff } }更多推荐
如何每天中午运行服务,并在每次启动
发布评论