【发布时间】:2017-04-03 09:18:04
【问题描述】:
我正在使用独立于 UI 的 Android 服务创建通知。这工作得很好。下面是代码。
public class SendNotificationService extends Service {
Context context;
String test_heading;
String test_body;
final class notifThread implements Runnable {
int service_id;
notifThread(int service_id) {
this.service_id = service_id;
}
@Override
public void run() {
String requested_method = "LoadBU";
String bu_status = "1";
CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
checkNewEntry.execute(requested_method, bu_status);
stopSelf(this.service_id);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new notifThread(startId));
thread.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
}
}
此服务也会在系统启动时自动启动。 CheckNewEntry 是我的 AsyncTask,它检查数据库并在有任何更改时发送通知。我没有添加CheckNewEntry,因为它超出了这个问题的范围。
现在我想做的是,每 30 秒或 1 分钟运行一次CheckNewEntry。
谁能帮忙?
【问题讨论】:
-
使用Handler方法
-
我是 Android 开发新手,能否修改我的代码?我一直在尝试这样做,但失败了。
-
这其实是我无法理解的。如何在我的代码中实现处理程序。