easycheese
easycheese

Reputation: 5899

Unable to start service intent Broadcast receiver Android

Like many others I get the following error of "Unable to start service Intent...not found"

I am setting a notification to run an activity once per day (in my tests it is set for 30 seconds). Here is the notification code:

Intent i = new Intent(this, OnNotificationReceiver.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.cancel(pi); // cancel any existing alarms

    am.setRepeating(AlarmManager.RTC_WAKEUP, notifyTime, AlarmManager.INTERVAL_FIFTEEN_MINUTES/30, pi);

OnNotificationReceiver.class is:

public class OnNotificationReceiver extends BroadcastReceiver {

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

        WakeReminderIntentService.acquireStaticLock(context);
        Intent i = new Intent(context, NotificationService.class);
        context.startService(i);
    }

NotificationService is:

public class NotificationService extends WakeReminderIntentService {
    public NotificationService() {
        super("ReminderService");
    }

    @Override
    void doNotificationWork(Intent intent) {
                 //Does work here
    }
}

WakeReminderIntentService is:

public abstract class WakeReminderIntentService extends IntentService {
abstract void doNotificationWork(Intent intent);

public static final String LOCK_NAME_STATIC = "com.companionfree.pricewatcher";
private static PowerManager.WakeLock lockStatic = null;

public static void acquireStaticLock(Context context) {
    getLock(context).acquire();
}

synchronized private static PowerManager.WakeLock getLock(Context context) {
    if (lockStatic==null) {
        PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

        lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOCK_NAME_STATIC);
        lockStatic.setReferenceCounted(true);
    }
    return(lockStatic);
}

public WakeReminderIntentService(String name) {
    super(name);
}

@Override
final protected void onHandleIntent(Intent intent) {
    try {
        doNotificationWork(intent);
    } finally {
        getLock(this).release();
    }
}
}

and my Manifest shows:

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

    <service android:name=".NotificationService"></service>
    <service android:name=".WakeReminderIntentService"></service>
</application>

Can anyone figure out where I went wrong?

Upvotes: 1

Views: 3394

Answers (1)

David Wasser
David Wasser

Reputation: 95626

It would have helped if you would have posted the logcat error (with stacktrace) as well.

In any case I can see that this code is wrong and is probably giving you that error:

Intent i = new Intent(this, OnNotificationReceiver.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms

You are using a PendingIntent for a Service. But you need an PendingIntent for a BroadcastReceiver. Try calling PendingIntent.getBroadcast() instead:

Intent i = new Intent(this, OnNotificationReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms

Upvotes: 1

Related Questions