我想在用户直接关闭其应用程序时更新用户的状态。
我试过了但没用:

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,它会将状态更新为“1”(1表示联机,0表示脱机)
这个应用程序也应该在后台运行,因此onstop和onpause排除了这个因素。

最佳答案

试试这个对我有用…

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

在清单中
<service
        android:name=".App_killed"
        android:stopWithTask="false" />

现在在主活动中启动服务;
startService(new Intent(getBaseContext(), App_killed.class));

现在在你的全球排球课上:
    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;
    }
}

09-30 17:53
查看更多