具有意图服务的 AsyncTask(带 AlarmManager)
AsyncTask with intent service (w AlarmManager)
我已经完成构建 android 天气应用程序。它使用 AsyncTask 从 api 获取天气信息,并通过调用适配器上的 notifyDataSetChanged() 在 onPostExecute() 中更新 UI。
现在我还想创建一个背景service/task等等。我知道AlarmManager。我想知道什么应该与 AlarmManager 结合使用来触发 AsyncTask。我担心这个问题的原因是我的 AsyncTask 也在更新 UI。但是,如果任何后台服务调用 AsyncTask,则前台没有 UI,因为当前应用程序不是 运行。会导致崩溃吗?
更新
在我的主要 activity 中,我调用此方法来启动我的警报管理器
public void scheduleAlarm() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 10000; // 5 seconds
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, pIntent);
}
警报管理器实施:
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, WeatherIntentService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
意图服务实现
@Override
protected void onHandleIntent(Intent intent) {
// Do the task here
Log.i("MyTestService", "Service running");
}
我对如何启动异步任务感到困惑。由于我的异步任务依赖于共享首选项以及从 gms 等接收到的位置。请引导我找到正确的路径。
因为没有UI,所以将你的Asynctask
放在Service
中,然后将你的数据存储在SQLite
中,这样你打开应用程序的那一刻>>你只需要从数据库中获取它。
这是一个示例流程
- 警报管理器将通知服务执行异步任务
- Asynctask 将从服务器或您的 API
收集信息
- 收集到的information/data应该存放在SQL
- 打开应用程序时,首先向数据库查询数据并更新UI。
- 执行常规应用程序
我已经完成构建 android 天气应用程序。它使用 AsyncTask 从 api 获取天气信息,并通过调用适配器上的 notifyDataSetChanged() 在 onPostExecute() 中更新 UI。
现在我还想创建一个背景service/task等等。我知道AlarmManager。我想知道什么应该与 AlarmManager 结合使用来触发 AsyncTask。我担心这个问题的原因是我的 AsyncTask 也在更新 UI。但是,如果任何后台服务调用 AsyncTask,则前台没有 UI,因为当前应用程序不是 运行。会导致崩溃吗?
更新 在我的主要 activity 中,我调用此方法来启动我的警报管理器
public void scheduleAlarm() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 10000; // 5 seconds
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, pIntent);
}
警报管理器实施:
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, WeatherIntentService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
意图服务实现
@Override
protected void onHandleIntent(Intent intent) {
// Do the task here
Log.i("MyTestService", "Service running");
}
我对如何启动异步任务感到困惑。由于我的异步任务依赖于共享首选项以及从 gms 等接收到的位置。请引导我找到正确的路径。
因为没有UI,所以将你的Asynctask
放在Service
中,然后将你的数据存储在SQLite
中,这样你打开应用程序的那一刻>>你只需要从数据库中获取它。
这是一个示例流程
- 警报管理器将通知服务执行异步任务
- Asynctask 将从服务器或您的 API 收集信息
- 收集到的information/data应该存放在SQL
- 打开应用程序时,首先向数据库查询数据并更新UI。
- 执行常规应用程序