关闭应用程序时asynctask不起作用

时间:2018-06-28 06:02:04

标签: android android-asynctask android-service android-recents

我当前正在开发一个视频会议应用程序,我想要实现的是当有人打电话给我时,我收到最近的应用程序后,通过从最近的应用程序中刷卡来关闭该应用程序,然后我想通过拒绝它来通知呼叫者从最近的应用程序关闭该应用程序时的通话

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停止。.我是新手Android,谢谢

1 个答案:

答案 0 :(得分:0)

即使应用已关闭,您也可以使用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);

将以上代码放在您要开始任何任务/执行任何特定任务的位置

然后您可以像这样创建Scheduler类:-

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;
    }
}