Как и многие другие, я получаю следующую ошибку: "Не удалось запустить служебное намерение... не найдено"
Я устанавливаю уведомление для запуска активности один раз в день (в моих тестах он установлен на 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
Вы используете PendingIntent
для Service
. Но вам нужен 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