Android 警报管理器服务不工作
Android Alarm Manager service does not work
我在这里找到了很多关于警报管理器的解决方案,但其中 none 似乎有效。
我创建后台服务:
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 class 是:
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 注释定义的 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" />
在过去的几个小时里看着这些线路促使我在这里寻求帮助。我希望你能看到我遗漏的东西。
查看控制台输出 "Sync scheduled."
首先,您的 <service>
元素在 android:name
属性中有一个恶意下划线,您需要将其删除。
其次,干掉android:process=":remote"
。为两行代码分叉整个过程 BroadcastReceiver
效率不高,并且会干扰下一个修复。
第三,从 BroadcastReceiver
切换到 WakefulBroadcastReceiver
并按照 class 的使用说明进行操作,因为现在设备很容易在服务工作之前或期间进入睡眠状态。
删除未使用的依赖项和多 dex 配置后已解决。对代码本身进行零更改,但是,它出于某些未知原因起作用。
我在这里找到了很多关于警报管理器的解决方案,但其中 none 似乎有效。
我创建后台服务:
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 class 是:
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 注释定义的 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" />
在过去的几个小时里看着这些线路促使我在这里寻求帮助。我希望你能看到我遗漏的东西。
查看控制台输出 "Sync scheduled."
首先,您的 <service>
元素在 android:name
属性中有一个恶意下划线,您需要将其删除。
其次,干掉android:process=":remote"
。为两行代码分叉整个过程 BroadcastReceiver
效率不高,并且会干扰下一个修复。
第三,从 BroadcastReceiver
切换到 WakefulBroadcastReceiver
并按照 class 的使用说明进行操作,因为现在设备很容易在服务工作之前或期间进入睡眠状态。
删除未使用的依赖项和多 dex 配置后已解决。对代码本身进行零更改,但是,它出于某些未知原因起作用。