在onDestroy方法中更新服务器上的状态

时间:2016-12-22 07:30:39

标签: android

当用户直接关闭 时,我希望更新状态。

我试过了,但这不起作用:

public class ExitService extends IntentService {

private static String TAG = ExitService.class.getSimpleName();

public ExitService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        String callNo = intent.getStringExtra("callNo");
        String status = intent.getStringExtra("status");
        updateExitStatus(callNo, status);
    }
}
public void updateExitStatus(final String callNo,final String status){
    StringRequest strReq1= new StringRequest(Request.Method.POST,
            Config.UTL_STATUS, new Response.Listener<String>(){
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("callNo", callNo);
            params.put("status",status);

            Log.e(TAG, "Posting params: " + params.toString());
            return params;
        }

    };

    // Adding request to request queue
    MyApplication.getInstance().addToRequestQueue(strReq1);
}

}

我有onResume会将状态更新为&#34; 1&#34; (在线为1,离线为0)

该应用也应该在后台运行,因此 onStop onPause 排除在这个等式之外。

2 个答案:

答案 0 :(得分:1)

试试这个对我有用......

 public class App_killed extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ClearFromRecentService", "Service Started");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ClearFromRecentService", "Service Destroyed");
    }

    public void onTaskRemoved(Intent rootIntent) {
        Log.e("ClearFromRecentService", "END");
        //Code here call your network call using volley/Asynch task..
        App_close();
        Toast.makeText(getApplicationContext(), "Warning: App killed", Toast.LENGTH_LONG).show();
        //stopSelf();
    }

    private void App_close() {
        // Tag used to cancel the request

        String tag_string_req = "close_app";

        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_CLOSE_APP, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d("close App", "Killed Response: " + response.toString());

                } catch (Exception e) {
                    // JSON error
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("close app", "Killed Error: " + error.getMessage());
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("status", status);
                params.put("mobile", callNo);
                return params;
            }

        };
        // Adding request to request queue
        VollyGlobal.getInstance().addToRequestQueue(strReq, tag_string_req);
    }
}

IN manifest

<service
        android:name=".App_killed"
        android:stopWithTask="false" />

现在在MainActivity中启动服务;

startService(new Intent(getBaseContext(), App_killed.class));

现在在你的VolleyGlobal课程中:

    public class VollyGlobal extends Application {

    private static Context context;

    public static final String TAG = VollyGlobal.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static VollyGlobal mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        VollyGlobal.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return VollyGlobal.context;
    }

    public static synchronized VollyGlobal getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    private Request<?> setDefaultRetryPolicy(Request<?> request) {
        request.setRetryPolicy(new DefaultRetryPolicy(0,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        return request;
    }
}

答案 1 :(得分:0)

在Intent Service&amp; Service中调用Server。在super.onDestory()之前从onDestroy调用该服务。

@Override
    protected void onDestroy() {
        startService(new Intent(this, ServerUpdateIntentService.class));
        super.onDestroy();
    }

对于Intent Service,请使用此链接:
https://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183