Android 每天使用服务(AlarmManager)或启动周期扣减更新存储内容

Android 每天使用服务(AlarmManager)或启动周期扣减更新存储内容,android,service,alarmmanager,ormlite,auto-update,Android,Service,Alarmmanager,Ormlite,Auto Update,我有联系人列表,我可以在一定时间内保存。我应该每天更新储蓄的条款。简单地说,减去一天的所有接触条款每天 我认为有两种可能的解决办法: 1) 服务,它将使用AlarmManager并每天在指定时间更新内容 2) 请记住上次启动应用程序时,检查差异并从存储项中扣除此差异 第二种方法在我看来更有效。有人能给点建议吗 如果您打算使用AlamManager每天运行一次您的服务,它将比性能/电池效率更高 如果不必在每天的确切时刻运行服务,您可以使用RTC而不是RTC\u WAKEUP,进一步提高性能 这样你

我有联系人列表,我可以在一定时间内保存。我应该每天更新储蓄的条款。简单地说,减去一天的所有接触条款每天

我认为有两种可能的解决办法:

1) 服务,它将使用AlarmManager并每天在指定时间更新内容

2) 请记住上次启动应用程序时,检查差异并从存储项中扣除此差异


第二种方法在我看来更有效。有人能给点建议吗

如果您打算使用AlamManager每天运行一次您的服务,它将比性能/电池效率更高

如果不必在每天的确切时刻运行服务,您可以使用
RTC
而不是
RTC\u WAKEUP
,进一步提高性能

这样你的应用程序就不会唤醒CPU,而是在用户再次使用手机时执行任务(如果闹钟已经晚了)。但是,如果这项服务一天不运行多次,你也不必太担心。可以把它想象成一个闹钟,每天都会响起来,不会耗尽你的电池。

我解决了我的问题

我使用的方法是在类中的onCreate()方法中调用的,该方法从应用程序类扩展而来:

private void setRecurringAlarm(Context context) {
               Intent intent = new Intent(AlarmReceiver.ACTION_ALARM);
                  AlarmManager alarms = (AlarmManager) this
                         .getSystemService(Context.ALARM_SERVICE);
               final PendingIntent pIntent = PendingIntent.getBroadcast(this,
                 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

               alarms.setRepeating(AlarmManager.RTC_WAKEUP,
                 System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, pIntent);

              Toast.makeText(getApplicationContext(), "started", Toast.LENGTH_SHORT).show();
    }
我的AlarmReceiver类:

  public class AlarmReceiver extends BroadcastReceiver {

    private static final String DEBUG_TAG = "AlarmReceiver";
    public static String ACTION_ALARM = "com.alarammanager.alaram";
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent downloader = new Intent(context, UpdateService.class);
        downloader.setAction(Constants.UPDATE_SERVICE);
        context.startService(downloader);
        Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
    }
}
以及我的服务,其中执行所需的操作:

      public class UpdateService extends IntentService {

        Boolean isRunning = false;
        private ContentRepository _contentRepo;
        public UpdateService() {
            super("UpdateService");
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            faq = new FrequentlyUsedMethods(this);
            _contentRepo = new ContentRepository(this.getContentResolver(), this);
            this.context = this;
            if (!isRunning)
                archiveDaysDeduct();

            return START_REDELIVER_INTENT;
        }

    private void archiveDaysDeduct() 
        {
            isRunning = true;
            Log.i("updateService", "archiveDaysDeduct");
            ArrayList<ContactItem> contacts = faq.getDisplayContacts(this);
         Toast.makeText(getApplicationContext(), "update service and I'm here", Toast.LENGTH_LONG).show();
            DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
            Dao<ContactItem,Integer> daoContact = null;

            String yourDate = faq.getCurrentDate();
            if (SharedPrefs.getInstance().getSharedPrefs()!=null)

                if (!SharedPrefs.getInstance().getSharedPrefs().getString(Constants.CURRENT_DATE, "").equalsIgnoreCase(yourDate))
                {       
SharedPrefs.getInstance().getSharedPrefs().edit().putString(Constants.CURRENT_DATE, yourDate);

                     try {
                         daoContact = db.getContactDao();
                     }
                     catch(Exception e)
                     {
                         e.printStackTrace();
                     }
                     Toast.makeText(getApplicationContext(), "I'm going to decrement the save dates", Toast.LENGTH_SHORT).show();
                    UpdateBuilder<ContactItem, Integer> updateBuilder = daoContact.updateBuilder();
                     DeleteBuilder<ContactItem, Integer> daoContactDelete = daoContact.deleteBuilder();
                     Log.i("contacts size in update service", Integer.toString(contacts.size()));
                    for (ContactItem a : contacts)
                    {
                        if (a.getDays()==0)
                        {
                            try {

                                daoContactDelete.where().eq("id", a.getId());
                                daoContact.delete(daoContactDelete.prepare());

                            } 
                            catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                        else 
                        {
                            try {
                                Log.i("contacts to update days", a.toString());
                                a.setDays(a.getDays()-1);
                                daoContact.update(a);
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    stopSelf();

                }
            }
        }
    }
公共类更新服务扩展了IntentService{
布尔值isRunning=false;
私有ContentRepository_contentRepo;
公共更新服务(){
超级(“更新服务”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
faq=新的常用方法(本);
_contentRepo=新的ContentRepository(this.getContentResolver(),this);
this.context=this;
如果(!正在运行)
ArchiveDaysDect();
返回启动\u重新交付\u意图;
}
私人作废归档文件()
{
isRunning=true;
Log.i(“更新服务”、“归档文件”);
ArrayList contacts=faq.getDisplayContacts(此);
Toast.makeText(getApplicationContext(),“更新服务,我在这里”,Toast.LENGTH\u LONG.show();
DatabaseHandler db=新的DatabaseHandler(context.getApplicationContext());
Dao-daoscontact=null;
字符串yourDate=faq.getCurrentDate();
if(SharedPrefs.getInstance().getSharedPrefs()!=null)
如果(!SharedPrefs.getInstance().getSharedPrefs().getString(Constants.CURRENT_DATE,“”).equalsIgnoreCase(yourDate))
{       
SharedPrefs.getInstance().getSharedPrefs().edit().putString(Constants.CURRENT_DATE,yourDate);
试一试{
daoContact=db.getContactDao();
}
捕获(例外e)
{
e、 printStackTrace();
}
Toast.makeText(getApplicationContext(),“我将减少保存日期”,Toast.LENGTH_SHORT.show();
UpdateBuilder UpdateBuilder=daoContact.UpdateBuilder();
DeleteBuilder daoContactDelete=daoContact.DeleteBuilder();
Log.i(“更新服务中的联系人大小”,Integer.toString(contacts.size());
用于(联系人项目a:联系人)
{
如果(a.getDays()==0)
{
试一试{
daoContactDelete.where().eq(“id”,a.getId());
daoContact.delete(daoContactDelete.prepare());
} 
捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
其他的
{
试一试{
Log.i(“联系人更新天数”,a.toString());
a、 setDays(a.getDays()-1);
更新(a);
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
stopSelf();
}
}
}
}

据我所知,当我启动服务时,我的应用程序将运行。即使它死掉了,对吗?是的,你的应用程序将运行,因为服务是你应用程序的一部分。但是,除非你以某种方式通知用户或从服务中启动一项活动,否则用户不会知道它正在运行?例如,如果您从活动开始服务,用户关闭活动如果您使用标志START_STICKY或NOT_STICKY等进行配置,服务将继续运行直到完成任务。但在极端情况下,如果内存/资源严重过载,并且您的应用计划关闭,android可以关闭您的服务,但在你的情况下,你应该担心这一点。