在特定的时间由每天不要alarmanager工作启动服务 [英] Start Service at specific time everyday by alarmanager dont work

查看:201
本文介绍了在特定的时间由每天不要alarmanager工作启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始在特定的时间独处日常然后服务的推出通知每分钟,以及服务做工精细服务,但其在被alarmmanager什么叫发生。

下面报警code:

 日历cur_cal =新的GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis的()); //设置该日历的当前时间和日期日历CAL =新的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));
意向意图=新意图(这一点,NotifService.class);
的PendingIntent pintent = PendingIntent.getBroadcast(这一点,0,意向,0);
AlarmManager报警=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),30 * 1000,pintent);

下面NotiService.Class:

 公共类NotifService延伸服务{    ScheduledExecutorService的调度= Executors.newSingleThreadScheduledExecutor();
    私有静态最终诠释HELLO_ID = 1;    @覆盖
    公众的IBinder onBind(意向为arg0){        返回null;
    }    @覆盖
    公共无效调用onStart(意向意图,诠释startId){        super.onStart(意向,startId);
        最终意图int​​ent1 =新意图(这一点,DialogoActivity.class);        scheduler.scheduleWithFixedDelay(新的Runnable(){            @覆盖
            公共无效的run(){                //查找通知管理服务器
                NotificationManager纳米=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);                //创建您的通知
                // INT图标= R.drawable.ic_launcher;
                CharSequence的tickerText =统治;
                //时长= System.currentTimeMillis的();
                //通知通知=新的通知(图标,tickerText时);
                上下文的背景下= getApplicationContext();
                CharSequence的contentTitle =轮到你了;
                字符串contentText =点击这里;                的PendingIntent pIntent = PendingIntent.getActivity(NotifService.this,0,intent1,0);                通知通知=新NotificationCompat.Builder(getApplicationContext()).setContentIntent(pIntent).setTicker(tickerText).setContentTitle(contentTitle).setContentText(contentText).setSmallIcon(R.drawable.ic_launcher).addAction(R.drawable.ic_launcher, SI,pIntent).setVibrate(新长[] {100,250,100,500})建立()。                notification.flags | = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
                //发送通知                nm.notify(HELLO_ID,通知);
            }
        },1,1,TimeUnit.MINUTES);
    }    @覆盖
    公共无效的onDestroy(){        super.onDestroy();
    }
}


解决方案

这可能是你没有你的服务的manifest.xml声明?

请参阅:的Andr​​oid,我的服务无法启动​​

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

解决方案

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

See: Android, my service wont start

这篇关于在特定的时间由每天不要alarmanager工作启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆