Android Alarm Manager服务不起作用

时间:2015-10-02 18:42:48

标签: android alarmmanager

我在这里找到了很多关于Alarm Manager的解决方案,但它们似乎都没有。

我用:

创建后台服务
public void scheduleSync() {
    Intent intent = new Intent(this, SyncReceiver.class);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), SYNC_INTERVAL, pendingIntent);
    Log.d(TAG, "Sync scheduled.");
}

SyncReceiver类是:

public class SyncReceiver extends BroadcastReceiver {
    private static final String TAG = "SyncReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, WebBackendSyncService_.class);
        context.startService(i);
        Log.d(TAG, "WebBackendSyncService started.");
    }
}

这就是使用Android Annotations定义的WebBackendSyncService:

@EIntentService
public class WebBackendSyncService extends IntentService {
    public static final String ACTION = "com.invoicing.networking.WebBackendSyncService";
    private static final String TAG = "WebBackendSyncService";

    @RestService
    APIService restClient;

    public WebBackendSyncService() { super(ACTION);}


    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "Handling sync intent.");
        sendInvoices();
    }

    @Background
    void sendInvoices() {
         SyncData.sendInvoices(restClient);
    }
}

清单中的服务和广播接收器:

<service
     android:name=".networking.WebBackendSyncService_"
     android:exported="false" />

 <receiver
     android:name=".networking.SyncReceiver"
     android:process=":remote" />

过去几个小时看着那条线,促使我在这里寻求帮助。我希望你会看到我遗失的东西。

查看控制台输出,它会进入“同步预定。”

2 个答案:

答案 0 :(得分:0)

首先,您的<service>元素在您需要删除的android:name属性中有一个流氓下划线。

其次,摆脱android:process=":remote"。为两行BroadcastReceiver分叉整个过程效率不高,并且会干扰下一个修复。

第三,从BroadcastReceiver切换到WakefulBroadcastReceiver并按照使用该类的说明进行操作,因为此时设备在服务工作之前或期间很容易入睡。

答案 1 :(得分:0)

删除未使用的依赖项和multi-dex配置后解决。对代码本身进行零更改,但是,它的工作原因不明。