无法克服它,无论我搜索多少。
我有 AlarmManager,我想在每 X 秒后启动 AsyncTask。
我确实有另一个文件,类 (Alarm.class) 我有 onReceive。无论我放入其中,都会完美激活。
现在我有问题。我的主线程/类(Onime)中有 AsyncTask。它执行许多任务,包括下载一些内容。
现在的问题是,我怎样才能从另一个类启动那个 AsyncTask。可悲的是静态不起作用,因为 asyc 包含一些不允许静态的东西。是否可以通过某种服务
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Put here YOUR code.
Log.d("alarminfo", "Alarm works!");
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
am.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 1000*60*1, pi); // Millisec * Second * Minute
Log.d("alarminfo", "Set alarm!");
}
public void CancelAlarm(Context context)
{
Log.d("alarminfo", "End alarm!");
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
那是警报类。
这是异步(它在 Onime.class 中(不同的文件):
class animeUpdator extends AsyncTask<ArrayList<Anime>, Integer, String> {
class Connection implements Runnable {
public void run () {
try {
URL url = new URL("http://xxx.com");
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
count = 1;
anime_list.clear();
while ((line = bufferedReader.readLine()) != null) {
parsingAnime(line);
count += 1;
}
bufferedReader.close();
MessageToMainThread(UPDATE_MAIN_GUI);
} catch(Exception e) {
Log.d("readInfo", e.toString());
}
}
}