0

一定期間保存できる連絡先のリストがあります。そして、貯金の条件を毎日更新する必要があります。簡単に言えば、毎日のすべての連絡先の条件に 1 日を差し引いたものです。

2 つの解決策が考えられます。

1) アラームマネージャーを利用し、毎日決まった時間にコンテンツを更新するサービス。

2) アプリケーションの最後の起動を記憶し、違いを確認し、保存された条件からこの違いを差し引きます..

2番目のアプローチは、パフォーマンスがより効率的であるように思えます。誰かが何かアドバイスできますか?

4

2 に答える 2

0

問題を解決しました。

クラスの onCreate() メソッドで呼び出されるメソッドを使用します。これは Application クラスから拡張されます。

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();

                }
            }
        }
    }
于 2013-08-27T11:44:39.947 に答える