电话重启的警报管理器

时间:2014-05-01 19:00:36

标签: android alarmmanager bootcompleted

我正在为我的java主题开发一个提醒应用程序。

我正在使用此代码创建待处理警报,这是代码。

Intent intent2 = new Intent(ReminderActivity.this, AlarmReceiver.class);
                intent2.putExtra("idint", iid);
                intent2.putExtra("title", myRem.getText().toString());
                intent2.putExtra("title_desc", myRemDesc.getText().toString());
                intent2.putExtra("date", date.toString());
                PendingIntent pendingIntent = 
                        PendingIntent.getActivity(ReminderActivity.this,iid, intent2,
                                PendingIntent.FLAG_CANCEL_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

我已经设置了我的AndroidManifest.xml以在启动时添加我的接收器

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="PollReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
</receiver>

最后,这是我的PollReceiver.class

public class PollReceiver extends BroadcastReceiver {
  static DBAdapter db;

  @Override
  public void onReceive(Context ctxt, Intent i) {
    scheduleAlarms(ctxt);
  }

  static void scheduleAlarms(Context ctxt) {
    db.open();
     Cursor c = db.getAllRecords();
     if(c.moveToFirst())
        {
            do{
                int iid = (int) c.getLong(0);
                String title = c.getString(1);
                String title_desc = c.getString(2);
                String date = c.getString(3);

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date myDate = null;
                    try {
                        myDate = sdf.parse(date);
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                Calendar cal = Calendar.getInstance();
                cal.setTime(myDate);

                Calendar CurCal = Calendar.getInstance();

                if (cal.compareTo(CurCal) <= 0 )
                {
                    Intent intent2 = new Intent(ctxt, AlarmReceiver.class);
                    intent2.putExtra("idint", iid);
                    intent2.putExtra("title", title);
                    intent2.putExtra("title_desc", title_desc);
                    intent2.putExtra("date", date);

                    PendingIntent pendingIntent = 
                            PendingIntent.getActivity(ctxt,iid, intent2,
                                    PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

            }

            } while(c.moveToNext());

        }

        db.close();

  }
}

但似乎仍然没有重新加载我的待处理警报。

任何狂野猜测的家伙?

1 个答案:

答案 0 :(得分:0)

如果你肯定你的警报配置正确,问题是你没有收到&#34; android.intent.action.BOOT_COMPLETE&#34;行动,然后计算:

  

作为谷歌的反恶意软件举措。谷歌一直主张这一点   用户应首先从启动器启动活动   应用程序可以做很多事情,防止BOOT_COMPLETED   交付,直到活动至少明确启动一次。

最近的android版本是一个常见的错误。

问候!