androidservicenotificationsbroadcastreceiveralarmmanager

Due to Android Service and AlarmManager app getting crashed


I'm trying to create a Service that will run in the background and put a notification in the status bar every 10 sec (for testing only).

I reviewed many posts here but still was unable to find help.

For some reason, when I call the AlarmManager to run the Service, the application crushing. Please advice.

This is the "Main Class" code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_screen);
    MyAlarmManager.scheduleAlarms(this);

This is the AlarmManager code:

public class MyAlarmManager extends BroadcastReceiver {

  private static final int PERIOD=1000*10; // 15 minutes
  private static final int INITIAL_DELAY=500; // 5 seconds

  @Override
  public void onReceive(Context ctxt, Intent i) {
      scheduleAlarms(ctxt);
  }

  static void scheduleAlarms(Context ctxt) {
    AlarmManager mgr= (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(ctxt, MyService.class);
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
    Log.i("My Log", "Alarm Manager Started ......... Alarm Manager Started");

    mgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + INITIAL_DELAY, PERIOD, pi);
  }
}

This is the "Service" code:

public class MyService extends Service {
private NotificationManager mNM;
private final IBinder mBinder = new LocalBinder();
private int NOTIFICATION = R.string.service_started;

public class LocalBinder extends Binder {
    SecRssService getService() {
        return SecRssService.this;
    }
}

@Override
public void onCreate() {
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Log.i("My Log", "Received onCreate Service !!!!!!!!!!!!!!!!!!!");

    showNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("My Log", "Received onStartCommand id " + startId + ": " + intent);
    Toast.makeText(this, R.string.service_started, Toast.LENGTH_SHORT).show();
    showNotification();

    // This service will continue running until it is explicitly stopped, so return sticky.
    return START_STICKY;
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    mNM.cancel(NOTIFICATION);
    Toast.makeText(this, R.string.service_stopped, Toast.LENGTH_SHORT).show();
}

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

private void showNotification() {

    CharSequence text = getText(R.string.service_started);

    Notification noti = new Notification.Builder(getBaseContext())
        .setContentTitle(getText(R.string.noto))
        .setContentText("Test Notification ON")
        .setSmallIcon(R.drawable.ic_launcher)
        .build();
}
}

This is my Log

08-02 11:09:48.872: E/AndroidRuntime(9550): Caused by: java.lang.ClassCastException: com.homeapps4u.sec_ticker_rss.MyService cannot be cast to android.content.BroadcastReceiver
08-02 11:09:48.872: E/AndroidRuntime(9550):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
08-02 11:09:48.872: E/AndroidRuntime(9550):     ... 10 more

Please advise my where I'm wrong.

Thanks a lot.


Solution

  • Thanks, I should have used:

            PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
    

    On MyAlarmManager.