0

I have tried to set an alarm in my android app. But it failed. I have read some tutorials but they don't work for me, i don't see where is my mistake.

Here is my code:

Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.byethost6.jessy_barthelemy.planificate">

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity android:name=".CreateTask"></activity>

        <receiver android:name="com.byethost6.jessy_barthelemy.planificate.HourReceiver" android:process=":remote"/>

    </application>

</manifest>

I set the alarm like this :

        AlarmManager alarmManager;
        PendingIntent alarmIntent;
        alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, HourReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);


        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();

And this is my broadcast receiver :

package com.byethost6.jessy_barthelemy.planificate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.byethost6.jessy_barthelemy.planificate.enumeration.TriggerEnum;
import com.byethost6.jessy_barthelemy.planificate.helper.Task;

public class HourReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "ALARM TRIGGERED", Toast.LENGTH_LONG).show();
    }
}

Could you please help me? :)

Ananta
  • 660
  • 1
  • 7
  • 19
  • What does "it failed" mean? Crash? Error? Warning? – NoChinDeluxe Jan 05 '16 at 20:47
  • Where you put your alarm? Because you see the "ALARM TRIGGERED" in the activity that manage the alarm. – Dario Jan 05 '16 at 20:50
  • Nothing happen. My alarm is set in the main activity – Ananta Jan 05 '16 at 21:02
  • Little suggestion: don't answere your question with "I have tried your code, but it doesn't work. The alarm doesn't start", but comment my answere :D . Have you write the permission that i post in my answere in the Manifest? – Dario Jan 05 '16 at 22:01
  • Ok i'm new on stackoverflow ^^. Yes I have put the permission. – Ananta Jan 05 '16 at 22:09
  • In addition to that I can't see anything concerning the alarm on the logcat output. – Ananta Jan 05 '16 at 22:19

1 Answers1

0

This is my Alarm in the MainActivity

alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

// Set the alarm to start at some time.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);

// Checking whether current hour is over 14
if (curHr >= 13)
{
    // Since current hour is over 14, setting the date to the next day
    calendar.add(Calendar.DATE, 1);
}

calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case,
// every day
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

And this is my BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag");

        //Acquire the lock
        wl.acquire();

        Log.v("ADebugTag", "It work!");

        //Release the lock
        wl.release();
    }
}

With this code in the Log you can see "It work" when the alarm fire!

The Manifest

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<receiver android:name=".MyBroadcastReceiver" />
Graphican0
  • 167
  • 11
Dario
  • 732
  • 7
  • 30