0

I am following the code posted HERE on setting up an alarm manager but I can't get it to work at all.

This code is in a class called "Alarm":

package com.sjjgames.abortionapp;
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.PowerManager;
    import android.widget.Toast;

    public class Alarm 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, "");
             wl.acquire();

             // Put here YOUR code.
             Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

             wl.release();
         }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pi); // Millisec * Second * Minute
     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
 }

I have added this code to the manifest:

<receiver android:process=":remote" android:name="Alarm"></receiver>
<service android:enabled="true" android:name="com.sjjgames.abortionapp.YourService" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

This is in a class called "YourService":

package com.sjjgames.abortionapp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();       
    }

    public void onStart(Context context,Intent intent, int startId)
    {
        alarm.SetAlarm(context);
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }
}

I expect a toast to display every 5 seconds.

Community
  • 1
  • 1
Shane
  • 972
  • 2
  • 12
  • 27
  • Good question but I am just following the tutorial in the link display above. The YourService class activates the alarm class. – Shane Jan 26 '13 at 01:02
  • At what point does your service gets triggered? I dont see any line where "YourService" starts. – Avinazz Jan 26 '13 at 05:03
  • Doesn't this line in the manifest trigger the "YourService" class? – Shane Jan 26 '13 at 05:44

1 Answers1

0

Your service doesn't call onStart() method. Instead of onStart() method you need to use onStartCommand():

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    alarm.setAlarm(this);
    return START_NOT_STICKY;
}

I suggest you to add @Override annotation to each overridden method in your classes. So if you add @Override to onStart() method the code will not be able to compile and error will be easier to spot.

J-rooft
  • 1,405
  • 1
  • 17
  • 18