Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.asusn46jv.sharepreference;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.view.View;
- import android.support.design.widget.NavigationView;
- import android.support.v4.view.GravityCompat;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class H_Admin extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_h__admin);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.addDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.h__admin, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- int id = item.getItemId();
- if (id == R.id.nav_camera) {
- // Handle the camera action
- } else if (id == R.id.nav_gallery) {
- } else if (id == R.id.nav_slideshow) {
- } else if (id == R.id.nav_manage) {
- } else if (id == R.id.nav_share) {
- } else if (id == R.id.nav_send) {
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- }
- package com.example.asusn46jv.sharepreference;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- Button btn_logout;
- TextView txt_id, txt_username;
- String id, username;
- SharedPreferences sharedpreferences;
- public static final String TAG_ID = "id";
- public static final String TAG_USERNAME = "username";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txt_id = (TextView) findViewById(R.id.txt_id);
- txt_username = (TextView) findViewById(R.id.txt_username);
- btn_logout = (Button) findViewById(R.id.btn_logout);
- sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
- id = getIntent().getStringExtra(TAG_ID);
- username = getIntent().getStringExtra(TAG_USERNAME);
- txt_id.setText("ID : " + id);
- txt_username.setText("USERNAME : " + username);
- btn_logout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // update login session ke FALSE dan mengosongkan nilai id dan username
- SharedPreferences.Editor editor = sharedpreferences.edit();
- editor.putBoolean(Login.session_status, false);
- editor.putString(TAG_ID, null);
- editor.putString(TAG_USERNAME, null);
- editor.commit();
- Intent intent = new Intent(MainActivity.this, Login.class);
- finish();
- startActivity(intent);
- }
- });
- }
- }
- package com.example.asusn46jv.sharepreference;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.ConnectivityManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.example.asusn46jv.sharepreference.app.AppController;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Map;
- public class Login extends AppCompatActivity {
- ProgressDialog pDialog;
- Button btn_register, btn_login;
- EditText txt_username, txt_password;
- Intent intent;
- int success;
- ConnectivityManager conMgr;
- private String url = Server.URL + "login1.php";
- private static final String TAG = Login.class.getSimpleName();
- private static final String TAG_SUCCESS = "success";
- private static final String TAG_MESSAGE = "message";
- public final static String TAG_USERNAME = "username";
- public final static String TAG_ID = "id";
- String tag_json_obj = "json_obj_req";
- SharedPreferences sharedpreferences;
- Boolean session = false;
- String id, username;
- public static final String my_shared_preferences = "my_shared_preferences";
- public static final String session_status = "session_status";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- {
- if (conMgr.getActiveNetworkInfo() != null
- && conMgr.getActiveNetworkInfo().isAvailable()
- && conMgr.getActiveNetworkInfo().isConnected()) {
- } else {
- Toast.makeText(getApplicationContext(), "No Internet Connection",
- Toast.LENGTH_LONG).show();
- }
- }
- btn_login = (Button) findViewById(R.id.btn_login);
- btn_register = (Button) findViewById(R.id.btn_register);
- txt_username = (EditText) findViewById(R.id.txt_username);
- txt_password = (EditText) findViewById(R.id.txt_password);
- // Cek session login jika TRUE maka langsung buka MainActivity
- sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
- session = sharedpreferences.getBoolean(session_status, false);
- id = sharedpreferences.getString(TAG_ID, null);
- username = sharedpreferences.getString(TAG_USERNAME, null);
- if (session) {
- Intent intent = new Intent(Login.this, MainActivity.class);
- intent.putExtra(TAG_ID, id);
- intent.putExtra(TAG_USERNAME, username);
- finish();
- startActivity(intent);
- }
- btn_login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String username = txt_username.getText().toString();
- String password = txt_password.getText().toString();
- // mengecek kolom yang kosong
- if (username.trim().length() > 0 && password.trim().length() > 0) {
- if (conMgr.getActiveNetworkInfo() != null
- && conMgr.getActiveNetworkInfo().isAvailable()
- && conMgr.getActiveNetworkInfo().isConnected()) {
- checkLogin(username, password);
- } else {
- Toast.makeText(getApplicationContext() ,"No Internet Connection", Toast.LENGTH_LONG).show();
- }
- } else {
- // Prompt user to enter credentials
- Toast.makeText(getApplicationContext() ,"Kolom tidak boleh kosong", Toast.LENGTH_LONG).show();
- }
- }
- });
- btn_register.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- intent = new Intent(Login.this, Register.class);
- finish();
- startActivity(intent);
- }
- });
- }
- private void checkLogin(final String username, final String password) {
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- pDialog.setMessage("Logging in ...");
- showDialog();
- StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.e(TAG, "Login Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- success = jObj.getInt(TAG_SUCCESS);
- // Check for error node in json
- if (success == 1) {
- String username = jObj.getString(TAG_USERNAME);
- String id = jObj.getString(TAG_ID);
- Log.e("Successfully Login!", jObj.toString());
- Toast.makeText(getApplicationContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
- // menyimpan login ke session
- SharedPreferences.Editor editor = sharedpreferences.edit();
- editor.putBoolean(session_status, true);
- editor.putString(TAG_ID, id);
- editor.putString(TAG_USERNAME, username);
- editor.commit();
- // Memanggil main activity
- Intent intent = new Intent(Login.this, MainActivity.class);
- intent.putExtra(TAG_ID, id);
- intent.putExtra(TAG_USERNAME, username);
- finish();
- startActivity(intent);
- } else {
- Toast.makeText(getApplicationContext(),
- jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- // JSON error
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Error: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting parameters to login url
- Map<String, String> params = new HashMap<String, String>();
- params.put("username", username);
- params.put("password", password);
- return params;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- }
Add Comment
Please, Sign In to add comment