每天在特定时间通过alarmmanager启动服务不起作用

时间:2014-11-24 19:55:08

标签: android service time alarmmanager android-alarms

我想在每天的特定时间开始服务,然后每分钟提供服务启动通知,单独的服务工作很好,但是当警报管理员调用时没有任何事情发生。

警报代码:

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 23);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(this, NotifService.class);
PendingIntent pintent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30 * 1000, pintent);

这里是NotiService.Class:

public class NotifService extends Service {

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private static final int HELLO_ID = 1;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

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

        super.onStart(intent, startId);
        final Intent intent1 = new Intent(this, DialogoActivity.class);

        scheduler.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {

                // Look up the notification manager server
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                // Create your notification
                //int icon = R.drawable.ic_launcher;
                CharSequence tickerText = "Dominion";
                //long when = System.currentTimeMillis();
                //Notification notification = new Notification(icon, tickerText, when);
                Context context = getApplicationContext();
                CharSequence contentTitle = "your turn";
                String contentText = "click here";

                PendingIntent pIntent = PendingIntent.getActivity(NotifService.this, 0, intent1, 0);

                Notification notification = new NotificationCompat.Builder(getApplicationContext()).setContentIntent(pIntent).setTicker(tickerText).setContentTitle(contentTitle).setContentText(contentText).setSmallIcon(R.drawable.ic_launcher).addAction(R.drawable.ic_launcher, "Si", pIntent).setVibrate(new long[] {100, 250, 100, 500}).build();

                notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
                // Send the notification

                nm.notify(HELLO_ID, notification);
            }
        }, 1, 1, TimeUnit.MINUTES);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:0)

可能是您没有在Manifest.XML中声明您的服务?

请参阅:Android, my service wont start