在特定时间运行广播

时间:2014-07-21 10:18:36

标签: android broadcast

    Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.YEAR, cur_cal.get(Calendar.YEAR));
    cal.set(Calendar.MINUTE, cur_cal.get(Calendar.MINUTE));
    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));
    // cal.set(Calendar.YEAR, value);
    Intent intent = new Intent(this, broadcastthepairs.class);
    PendingIntent pintent = PendingIntent.getService(
            this.getApplicationContext(), 234324243, intent, 0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, (cal.getTimeInMillis() + 3000),
            pintent);

主要节日

<receiver android:name="cryptsy.pricelist.broadcastthepairs" >

    </receiver>

好吧所以我想开始一个广播,然后在15点开始服务:某个时间。 我从网上搜索过这个代码,所有内容与此类似,但是我无法让它们正常工作,你能告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

您正在创建错误的PendingIntent。您应该使用getBroadcast()代替getService()

PendingIntent pintent = PendingIntent.getBroadcast(
        this.getApplicationContext(), 234324243, intent, 0);