无论我搜索多少,都无法克服它。
我有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);
}
}
多数警报等级。
这是Async(它在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());
}
}
}
答案 0 :(得分:0)