Reputation: 159
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);
Upvotes: 3
Views: 280
Reputation: 28103
Your code is perfectly fine.
Only thing you make sure your java files are located in same package package="iiriondo.activity"
Upvotes: 1
Reputation: 15699
can try with passing the to passing "this" instaed of the getApplicationContext()
new Intent(this,OnAlarmReceiver.class);
Upvotes: 0
Reputation: 22291
Write Below Intent Code instead of your intent code.
Intent intent = new Intent(MainActivity.this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (5 * 1000),pendingIntent);
Upvotes: 0