alarmmanager android运行不正常

时间:2014-12-30 23:20:59

标签: android alarmmanager

我想在用户按下按钮

后每隔X天运行一次任务

我的代码但它不能与我合作

OnButtonClick

                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, Calendar.YEAR);
                calendar.set(Calendar.MONTH, Calendar.MONTH);
                calendar.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);
                calendar.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
                calendar.set(Calendar.MINUTE, Calendar.MINUTE);
                calendar.add(Calendar.MINUTE, 1);

                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent i = new Intent(ActionsActivity.this, SuggestService.class);
                PendingIntent pi = PendingIntent.getService(ActionsActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
                //startService(i);

警报类

public class Alarm extends BroadcastReceiver 
{    
 @SuppressLint("Wakelock")
 @Override
 public void onReceive(Context context, Intent intent) 
 {   
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
     wl.acquire();

     // Put here YOUR code.
     Toast.makeText(context, "Alarm !!!", Toast.LENGTH_LONG).show();

     wl.release();
 }

 public void SetAlarm(Context context)
 {
     AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10 * 1, pi); // Millisec * Second * Minute
 }

服务类

public class SuggestService extends Service
{
public void onCreate()
{
    super.onCreate();       
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}


@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    Toast.makeText(getBaseContext(), "Alarm !!!", Toast.LENGTH_LONG).show();

    wl.release();
}

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}
}

立刻点击Toast秀! :(

任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:0)

我认为问题到了,因为警报在设置后立即触发。

我的代码中遇到了这个问题,我通过稍微改变闹钟时间来征服它:

am.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 5*1000,
                MAINTENANCE_INTERVAL,
                pendingIntent);

答案 1 :(得分:0)

至少有一个问题是您正在使用的Calendar。这一行:

            Calendar calendar = Calendar.getInstance();

获取当前时间的Calendar实例。然后用这一行将年份设置为“1”......

            calendar.set(Calendar.YEAR, Calendar.YEAR);

因为Calendar.YEAR =“1”......而这些其他行也做类似的“怪异”设置。

            calendar.set(Calendar.MONTH, Calendar.MONTH);
            calendar.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);
            calendar.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
            calendar.set(Calendar.MINUTE, Calendar.MINUTE);

不要使用其中任何一个,只需这样做:

            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MINUTE, 1);