Start Service at specific time everyday by alarmanager dont work

荒凉一梦 提交于 2019-12-13 04:47:16

问题


I want to start a service at specific time everyday and then service launch notification every min, well service work fine alone but when its called by the alarmmanager nothing happens.

Here alarm code:

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

Here 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:


It might be that you don't have your Service declared in Manifest.XML?

See: Android, my service wont start



来源:https://stackoverflow.com/questions/27113064/start-service-at-specific-time-everyday-by-alarmanager-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!