-4

i am creating the login application,using php in android studio, my question is how to keep the application online.

i.e

when i login the app using the username and password,the app get logged in,when when i close the app. it get logged out,but i need to keep the app logged in when i close the app. also.

can any one suggest how to do that?

this is my 1st main activity (login activity)

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends Activity {

    EditText name, password;
    String Name, Password;
    Context ctx=this;
    String NAME=null, PASSWORD=null, EMAIL=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.main_name);
        password = (EditText) findViewById(R.id.main_password);
    }


    public void main_login(View v){
        Name = name.getText().toString();
        Password = password.getText().toString();
        BackGround b = new BackGround();
        b.execute(Name, Password);
    }

    class BackGround extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String name = params[0];
            String password = params[1];
            String data="";
            int tmp;

            try {
                URL url = new URL("http://localhost/sample/Login%20php/login.php");
                String urlParams = "name="+name+"&password="+password;

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                os.write(urlParams.getBytes());
                os.flush();
                os.close();

                InputStream is = httpURLConnection.getInputStream();
                while((tmp=is.read())!=-1){
                    data+= (char)tmp;
                }

                is.close();
                httpURLConnection.disconnect();

                return data;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "Exception: "+e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {

            if(result.equals("Matches")) {

               // Intent intent=new Intent(Intent.ACTION_VIEW);
                //intent.setData(Uri.parse("content://com.android.calendar/time/"));
                //startActivity(intent);

                Boolean Registered; final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                Registered = sharedPref.getBoolean("Registered", false);
                if (!Registered)
                {
                        startActivity(new Intent(MainActivity.this,MainActivity.class));
                }else
                    { startActivity(new Intent(MainActivity.this,Main2Activity.class));
                    }
            }

            if(result.equals("No match")) {

                Toast.makeText(MainActivity.this, "USERNAME/PASSWORD IS INCORRECT", Toast.LENGTH_LONG).show();
            }

        }
    }
}

2nd activity

    package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPref.edit(); editor.putBoolean("Registered", true);
        editor.apply();


    }

}
selva s
  • 29
  • 1
  • 9

1 Answers1

0

in onPostExecute of your MainActivity

        @Override
            protected void onPostExecute(String result) {

                if(result.equals("Matches")) {

    startActivity(new Intent(MainActivity.this,Main2Activity.class)); 
  finish();              
                }

                if(result.equals("No match")) {

                    Toast.makeText(MainActivity.this, "USERNAME/PASSWORD IS INCORRECT", Toast.LENGTH_LONG).show();
                }

            }
        }

Create a splash screen and make it launcher activity in manifest

public class Splash extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Boolean Registered;
            final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Splash.this);
            Registered = sharedPref.getBoolean("Registered", false);

            if (!Registered)
            {
                startActivity(new Intent(Splash.this,MainActivity.class));
            }else {
                startActivity(new Intent(Splash.this,SecondAcivity.class));
            }

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}

Your Main2Activity Activity is correct

Kapil Parmar
  • 816
  • 7
  • 18
  • which is your launcher activity? – Kapil Parmar Jan 23 '18 at 07:00
  • 1st activity is Mainactivity activity sir,which i have given above! my question is where to use your code in mainactivity,if the user logged in i have to display 2nd activity,every time he closes and opens the app it should display 2nd activity when he already logged in. – selva s Jan 23 '18 at 07:05
  • yes sir i had changed it,please check it weather i had used it correct or not,i had edited the code – selva s Jan 23 '18 at 07:25
  • but i am facing a problem like i cannot find my app in my phone or emulator after installing and running the app.but when i close the app. to open t again i cannot find the app in emulator. – selva s Jan 23 '18 at 07:26
  • thank you for helping me sir,its working thank you so much!i have another doubt also can i ask you?can you help me? – selva s Jan 23 '18 at 07:52
  • nothing sir,when i created the logging app,i logged in and went to next activity,but when i use the back button in emuator/mobile it again goes to the login page, now only i checked your code solved 2 of my problems not going back to logging screen & staying logged in. Thank you so much sir! spending time for me! – selva s Jan 23 '18 at 08:02
  • while to next Activity use finish(); after startActivity(new Intent(MainActivity.this,Main2Activity.class)); – Kapil Parmar Jan 23 '18 at 08:05