应用关闭时如何运行BackgroundTask

时间:2019-03-06 15:37:14

标签: php android json

我想在关闭应用程序时运行backgroundtask,而我想在用户关闭应用程序时运行logout.php

这是我的登录代码以及我如何运行Backgroundtask,我正在使用 ECLIPSE

很抱歉我的新手问题,我只是想学习Android,请帮助我

 AlertDialog alertDialog;  
 Context ctx; 
 ProgressDialog nDialog;
 String JSON_STRING;
    String json_string;
    private class BackgroundTask extends AsyncTask<String, Void, String> {
        String JSON_URL;
        String un,up;
        @Override
        protected void onPreExecute() {
            JSON_URL ="https://ralph-rojas1996.000webhostapp.com/android/login1.php";
        }

        @Override
        protected String doInBackground(String... args) {
           un = args[0];
           up = args[1];
            try {
                StringBuilder JSON_DATA = new StringBuilder();
                URL url = new URL(JSON_URL);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                String data_string = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(un,"UTF-8")+"&"+
                        URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(up,"UTF-8");
                        bufferedWriter.write(data_string);
                        bufferedWriter.flush();
                        bufferedWriter.close();
                        outputStream.close();
                InputStream  in = httpURLConnection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                while ((JSON_STRING = reader.readLine())!=null) {
                    JSON_DATA.append(JSON_STRING).append("\n");
                }
                String x = JSON_DATA.toString().trim();
                if(x.equals("1"))
                {
                    return null;
                }
                else if(x.equals("2"))
                {
                    la = "2";
                    return la;
                }
                else
                {
                return JSON_DATA.toString().trim();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;


        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {

            json_string = result;
            onLoginResponse(json_string);
        }
    }
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    getActionBar().hide(); 
    setContentView(R.layout.activity_main);
    B1 = (Button)findViewById(R.id.btnloginok);
    t1 = (TextView)findViewById(R.id.name);
    t2 = (TextView)findViewById(R.id.pname);
    //new BackgroundTask().execute();
    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if(networkInfo!=null && networkInfo.isConnected())
    {
        t2.setVisibility(View.INVISIBLE);
        t1.setVisibility(View.VISIBLE);
    }
    else
    {
        B1.setEnabled(false);
        t2.setVisibility(View.VISIBLE);
        t1.setVisibility(View.INVISIBLE);
    }
    un = (EditText)findViewById(R.id.editText1);
    pass = (EditText)findViewById(R.id.editText2);


}



public void login(View v)
{
    String uname = un.getText().toString();
    String upass = pass.getText().toString();
    BackgroundTask backgroundTask = new BackgroundTask();
    backgroundTask.execute(uname,upass);

     nDialog = new ProgressDialog(this);
     nDialog.setMessage("Loading..");
     nDialog.setTitle("Get Data");
     nDialog.setIndeterminate(false);
     nDialog.setCancelable(true);
     nDialog.show();
    /*final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after 5s = 5000ms
            if(json_string==null)
            {
                Toast.makeText(getApplicationContext(), "Login Failed Try Again..", Toast.LENGTH_LONG).show();

            }
            else
            {
                //Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this,intro.class);
                intent.putExtra("json_data", json_string);
                startActivity(intent);

            }
        }
    }, 4000);*/


}
private void onLoginResponse(String json){
    if(json==null)
    {
        nDialog.dismiss();
        Toast.makeText(getApplicationContext(), "Login Failed Try Again..", Toast.LENGTH_LONG).show();
    }
    else if (json=="2")
    {
        nDialog.dismiss();
        Toast.makeText(getApplicationContext(), "User is Already Logged In on other Device", Toast.LENGTH_LONG).show();
    }
    else
    {
        nDialog.dismiss();
        //Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(MainActivity.this,intro.class);
        intent.putExtra("json_page1", json);
        startActivity(intent);
    }
}



public void signup (View v)
{
    Intent i = new Intent (this,signup.class);
    startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我想在用户关闭应用程序时自动运行logout.php。

0 个答案:

没有答案