Android AlarmManager无法工作一天

时间:2016-04-18 09:21:44

标签: java android alarmmanager

我正在为我的应用创建闹钟。警报是假设在早上8点左右唤醒应用程序并拨打服务器电话。当我将时间更改为一小时或更短时间时,它可以获取数据并发送通知。但是,当我将日历扩展到一天的间隔时,它不起作用。这是我的代码。

设置闹钟:

private void SetAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 9);

    Intent intent = new Intent(this, TodayWordReceiver.class);
    boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(),
            0, intent, PendingIntent.FLAG_NO_CREATE) != null);
    if (!alarmUp) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

        Log.i("Alarm", "alarm set success");
    } else {
        Log.i("Alarm", "Already created");
    }
}

TodayWordReceiver:

public class TodayWordReceiver extends BroadcastReceiver {
public TodayWordReceiver() {}

@Override
public void onReceive (Context context, Intent intent) {
    WakeLocker.acquire(context);
    Log.i("Receiver", "Called here");
    Intent serIntent1 = new Intent(context, FetchTodayWordService.class);
    context.startService(serIntent1);

}

}

FetchTodayService:

public class FetchTodayWordService extends IntentService {

WordDay wordDay;
final String url =
        "http://api.wordnik.com:80/v4/words.json/wordOfTheDay?api_key=KEY";
public FetchTodayWordService () {
    super("FetchTodayWordService");
}

@Override
protected void onHandleIntent (Intent intent) {
    Log.i("OUTPUT", "Service called");
    MakeRequest();
}
public void MakeRequest()
{
    JsonObjectRequest jsonObjectRequest =
            new JsonObjectRequest
                    (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse (JSONObject response) {
                            wordDay = ParseWordDay.ParseJSON(response);
                            if(wordDay != null)
                            {
                                new StoreData().DoWork(getApplicationContext(), wordDay);
                            }

                        }
                    },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse (VolleyError error) {
                                    Log.i("OUTPUT", String.valueOf(error));


                                }
                            });
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}

WakeLocker:

public class WakeLocker {
private static PowerManager.WakeLock wakeLock;

public static void acquire(Context ctx) {
    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "LIGHT APP");
    wakeLock.acquire();
}

public static void release() {
    if (wakeLock != null) wakeLock.release(); wakeLock = null;
}

}

Manifest Permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

0 个答案:

没有答案