3

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);   
Cœur
  • 37,241
  • 25
  • 195
  • 267
mai87
  • 159
  • 2
  • 11
  • 1
    "but it doesn't work" is not very much. What do you want to achieve, which error do you get? What tells you your debugger? – Shegit Brahm Jun 14 '12 at 09:52
  • see this answer http://stackoverflow.com/questions/8999103/isuues-alarm-manager-in-every-1-min-android/8999344#8999344 – Ajay Jun 14 '12 at 10:04

3 Answers3

1

Your code is perfectly fine.

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

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • Ouch!! You are right. The problem was that the class OnAlarmReceiver was located in other package. Thanks. – mai87 Jun 17 '12 at 16:00
0

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);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

can try with passing the to passing "this" instaed of the getApplicationContext()

new Intent(this,OnAlarmReceiver.class);
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36