2012-07-15 114 views
1

和很多人一樣我得到以下錯誤「無法啓動服務意向......找不到」無法啓動服務意向廣播接收器的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(); 
    } 
} 
} 

和我的清單笑ws:

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

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

任何人都可以找出我出錯的地方嗎?

回答

1

如果你也發佈了logcat錯誤(帶有堆棧跟蹤),它會有所幫助。

在任何情況下,我可以看到,這個代碼是錯誤的,並可能給出這樣的錯誤:

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 

您使用的是PendingIntentService。但是您需要PendingIntent作爲BroadcastReceiver。嘗試調用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 
+0

呀,我才發現錯誤。但仍然有問題。我終於可以在清單和意圖中使用完整的類名了。 – easycheese 2012-07-15 22:00:36