gpt4 book ai didi

android - 当应用程序在 android 上关闭时,使用 alarmmanager 设置的 Asynctask 失败

转载 作者:行者123 更新时间:2023-11-30 03:05:01 25 4
gpt4 key购买 nike

当我的应用程序打开时,我有一个运行良好的 ASyncTask 但您知道什么时候按住手机上的主页按钮并通过滑动关闭应用程序吗?它崩溃了。

我的 AlarmReceiver

 public class Alarm-Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
final Context errorContext = context;
final UP refUP = new UP(); //just an activity for my toast

class NotifRequest extends AsyncTask<String, Void, String[]> {
String NotifUrlStr = String.format(errorContext.getString(R.string.notification_url), Main.Text);

@Override
protected String[] doInBackground(String... params) {
String[] On = new String[2];
String onProd = "";
String sonOnTar = "";


try {
JSONTokener onTokener = new JSONTokener(Sources.httpConnGet(NotifUrlStr).toString());
JSONArray onArray=new JSONArray(onTokener);


JSONObject json_obj_on = onArray.getJSONObject(0);

onProd = json_obj_on.getString("on");
if(onProd .equals("0") == false) {
sonOnTar = json_obj_on.getString("sonon");
}

} catch (Exception e) {

refUP.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(errorContext, "Error!", Toast.LENGTH_LONG).show();
}
});
CancelAlarm(errorContext);
}

On[0] = onProd;
On[1] = sonOnTar;
return On;
}

@Override
protected void onPostExecute(String[] onRes) {

String onSay = onRes[0];
String onTar = onRes[1];

if (onSay.equals("0") && onSay != null) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(errorContext)
.setSmallIcon(R.drawable.akilli_launcher)
.setContentTitle("New")
.setContentText("new");

Intent resultIntent = new Intent(errorContext, UPSiparisler.class);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
errorContext,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);


int mNotificationId = 001;

NotificationManager mNotifyMgr =
(NotificationManager) errorContext.getSystemService(Context.NOTIFICATION_SERVICE);

mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

super.onPostExecute(onRes);
}

}


NotifRequest notReq = new NotifRequest();
notReq.execute();

}

public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmNotifReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pi); // Millisec * Second
}

public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmNotifReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}


}

即使在应用程序关闭并且不继续显示 Toast“错误”

后,我应该怎么做才能使其正常工作

提前致谢。

最佳答案

在您的 Activity 中,当 Activity 进入停止状态时,您还应该尝试取消 AsyncTask,如下所示:

  @Override
public void onStop() {
super.onStop();
notReq.cancel(true);
}

我假设当 Activity 不可见时,您不需要 AsyncTask 来完成。

关于android - 当应用程序在 android 上关闭时,使用 alarmmanager 设置的 Asynctask 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com