关闭应用程序时异步任务不起作用
Posted
技术标签:
【中文标题】关闭应用程序时异步任务不起作用【英文标题】:asynctask not working when app is closed 【发布时间】:2018-12-07 02:39:05 【问题描述】:我目前正在开发一个视频会议应用程序,我想要实现的是,当有人打电话给我并接到他们的电话时,我通过从最近的应用程序中滑动来关闭应用程序,然后我想通过拒绝来通知来电者从最近的应用关闭应用时的调用
public class RecentAppService extends Service
private static final String TAG = "RecentAppStatus";
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public void onCreate()
super.onCreate();
Log.i(TAG, "onCreate()");
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.i(TAG, "onStartCommand()");
return START_STICKY;
@Override
public void onTaskRemoved(Intent rootIntent)
super.onTaskRemoved(rootIntent);
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!!");
Boolean callactive= AppsharedPreference.getAppPrefrerence(this)
.readBooleanData("callingactive");
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Callactive ::::"+callactive);
if(callactive)
NotificationManager nManager = ((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
rejectCall();
Intent restartService = new Intent(getApplicationContext(), this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
@Override
public void onDestroy()
super.onDestroy();
Log.i(TAG, "onDestroy()");
@Override
public void onLowMemory()
super.onLowMemory();
Log.i(TAG, "onLowMemory()");
public void rejectCall()
AsyncTaskRunner runner = new AsyncTaskRunner(this);
runner.execute();
当有人从最近的应用程序中关闭我的应用程序时,我添加了上面的 Sticky 服务来执行任务,我还在服务中添加了包含 Asynctask 的以下代码以执行拒绝呼叫功能
class AsyncTaskRunner extends AsyncTask<String, String, String>
private String resp;
private Context mcontext;
public AsyncTaskRunner(Context mContext)
this.mcontext=mContext;
@Override
protected String doInBackground(String... params)
publishProgress("Sleeping..."); // Calls onProgressUpdate()
try
String mUserHash= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("mUserHash");
String rejectApi= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("rejectApi");
String apiToken= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("apiToken");
String room= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("room");
String callId= AppsharedPreference.getAppPrefrerence(mcontext)
.readStringData("callId");
ApiCallData apiCallData = new ApiCallData(rejectApi, apiToken, room, callId,mUserHash);
WebService.getWebservice().rejectCall(mcontext, apiCallData, new FutureCallback<String>()
@Override
public void onCompleted(Exception e, String result)
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Result"+result);
);
catch (Exception e)
e.printStackTrace();
resp = e.getMessage();
return resp;
@Override
protected void onPostExecute(String result)
Log.i(TAG, "onTaskRemoved()....!!!!!!!!!! Result"+result);
@Override
protected void onPreExecute()
@Override
protected void onProgressUpdate(String... text)
一切正常,当应用程序不在前台但应用程序打开时,调用在调用方正确断开,然后在 Asynctask 中未完成任务,即,Asynctask 在应用程序关闭时停止。我是新手安卓,谢谢
【问题讨论】:
你应该使用服务上下文而不是应用上下文 是的,你是对的,我已经在我的代码中更正了,但这并不能解决我的问题 我没有看到您正在检查应用程序生命周期的任何代码? 您不应将AsyncTask
与Service
一起使用。看到这个:***.com/a/45653186/6383029
【参考方案1】:
即使应用程序关闭,您也可以使用FirebaseJobDispatcher
执行某些操作。
要使用FirebaseJobDispatcher
,首先你需要创建一个 Job ,它是这样的:-
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
.setService(SyncJobScheduler.class)
.setTrigger(Trigger.executionWindow(0, 0))
.setTag("Set_your_unique_tag_here")
.setReplaceCurrent(false)
.build();
dispatcher.mustSchedule(myJob);
把上面的代码放在你想开始任何任务/执行任何特定任务的地方
然后您可以像这样创建您的调度程序类:-
public class SyncJobScheduler extends JobService
@Override
public boolean onStartJob(final JobParameters job)
new Thread(new Runnable()
@Override
public void run()
try
if (("Set_your_unique_tag_here").equalsIgnoreCase(job.getTag()))
//PERFORM YOUR TASK HERE_ WHICH YOU WANT TO RUN IN ASYNCTASK OR SERVICE
//IT WILL RUN EVEN IF APP IS IN BACKGROUND /CLOSED
catch (Throwable ignored)
).start();
return false;
@Override
public boolean onStopJob(JobParameters job)
return false;
【讨论】:
以上是关于关闭应用程序时异步任务不起作用的主要内容,如果未能解决你的问题,请参考以下文章