2

我知道当您onReceive()BroadcastReceiver.

但是,这是 my 的代码onReceive(),建议:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Iniciando classes   
    Intent iLocator = new Intent(context, LocatorService.class);
    context.startService(iLocator);
}

Intent 是通过这个方法发送的:

public void agendarPing() {
    Intent it = new Intent("EXECUTA");
    PendingIntent p = PendingIntent.getBroadcast(LocatorService.this, 0,
            it, 0);

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.add(Calendar.SECOND, 360);

    long tempoReabrir = c.getTimeInMillis();
    AlarmManager reabrir = (AlarmManager) getSystemService(ALARM_SERVICE);
    reabrir.set(AlarmManager.RTC_WAKEUP, tempoReabrir, p);
    Log.d(TAG, "Alarme agendado com sucesso!");
    stopSelf();
}

为什么我要有这个日志?

04-24 08:41:40.742: W/ActivityManager(1483): Timeout of broadcast BroadcastRecord{40b3f1c0 EXECUTA} - receiver=android.os.BinderProxy@40bcfef0, started 20008ms ago

04-24 08:41:40.742: W/ActivityManager(1483): Receiver during timeout: ResolveInfo{40b78208 br.com.contele.locator.LocatorReceiver p=0 o=0 m=0x108000}
4

1 回答 1

0

Intent.FLAG_ACTIVITY_NEW_TASK在这里没用,你开始一个Service不是Activity.

另外,为什么要间接启动服务?单独的 Pending Intent 可以代表您启动服务:

PendingIntent pi = PendingIntent.getService(mContext,1001,new Intent(LocatorService.class),PendingIntent.FLAG_CANCEL_CURRENT);

将此挂起的缩进传递给AlarmManager.

于 2013-04-24T15:42:38.360 回答