Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alarm doesn't work

I am doing an alarm application, I take code of examples I have found in Internet but it doesn't work I don't know why.

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="iiriondo.activity"
  android:versionCode="1"
  android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".LoginActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".OnAlarmReceiver" ></receiver>

</application>
</manifest>

Here the class that listen to the alarm:

public class OnAlarmReceiver extends BroadcastReceiver{

private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "La Alarma está sonando",Toast.LENGTH_LONG).show();

}

}

And finally I use this code for set the Alarm:

 Intent intent = new Intent(getApplicationContext(),OnAlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 1);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (5 * 1000), pendingIntent);   
like image 953
mai87 Avatar asked Dec 06 '25 10:12

mai87


1 Answers

Your code is perfectly fine.

Only thing you make sure your java files are located in same package package="iiriondo.activity"

like image 183
Vipul Avatar answered Dec 07 '25 22:12

Vipul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!