广播BroadcastRecord的超时只是调用服务

时间:2013-04-24 14:53:56

标签: android broadcastreceiver alarmmanager

我知道当你在onReceive()的{​​{1}}方法中进行一些繁重的处理时,会抛出这种类型的关闭日志。

但是,这是我BroadcastReceiver的代码,建议使用:

onReceive()

通过此方法发送Intent:

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

我为什么要记录这个日志?

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

1 个答案:

答案 0 :(得分: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