簡體   English   中英

無法啟動服務意圖廣播接收器Android

[英]Unable to start service intent Broadcast receiver Android

像許多其他人一樣,我得到以下錯誤:“無法啟動服務意圖...未找到”

我設置了每天運行一次活動的通知(在我的測試中,該活動設置為30秒)。 這是通知代碼:

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是:

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是:

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

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

WakeReminderIntentService是:

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();
    }
}
}

我的清單顯示:

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

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

誰能弄清楚我哪里出了問題?

如果您還發布了logcat錯誤(帶有stacktrace),那將會有所幫助。

無論如何,我可以看到此代碼是錯誤的,並且可能會給您該錯誤:

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

您正在為Service使用PendingIntent 但是您需要一個BroadcastReceiverPendingIntent 嘗試改為調用PendingIntent.getBroadcast()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM