【发布时间】:2015-08-30 14:52:14
【问题描述】:
所以,我已经通过互联网寻找解决方案,但没有找到任何结合我需要的东西。
我需要这个: 如果手机重新启动,我需要在启动时启动一项服务,该服务将每 15 分钟向我的服务器发送数据。我编写了可用于启动的代码,但不知道如何实现计时器。 我需要 2 个广播接收器和 2 个服务吗?
我的广播接收器:
public class BrReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, Service.class);
context.startService(serviceIntent);
}
}
}
和我的服务:
public class Service extends IntentService {
private static final String TAG = "DownloadService";
public Service() {
super(Service.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Service Started!");
}
}
还有我的 AndroidManifest:
<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
<receiver android:name=".services.BrReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".services.Service"
android:exported="false" />
这项每 15 分钟重复一次的服务不能从 Activity 开始,而是在启动时开始。
【问题讨论】:
标签: android service timer broadcastreceiver android-intentservice