Guest User

Untitled

a guest
Sep 15th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.17 KB | None | 0 0
  1. package com.example.asusn46jv.sharepreference;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.view.View;
  9. import android.support.design.widget.NavigationView;
  10. import android.support.v4.view.GravityCompat;
  11. import android.support.v4.widget.DrawerLayout;
  12. import android.support.v7.app.ActionBarDrawerToggle;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.support.v7.widget.Toolbar;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.widget.TextView;
  18.  
  19. public class H_Admin extends AppCompatActivity
  20. implements NavigationView.OnNavigationItemSelectedListener {
  21.  
  22.  
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_h__admin);
  28. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  29. setSupportActionBar(toolbar);
  30.  
  31.  
  32. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  33. fab.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  37. .setAction("Action", null).show();
  38. }
  39. });
  40.  
  41. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  42. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  43. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  44. drawer.addDrawerListener(toggle);
  45. toggle.syncState();
  46.  
  47. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  48. navigationView.setNavigationItemSelectedListener(this);
  49.  
  50.  
  51.  
  52. }
  53.  
  54. @Override
  55. public void onBackPressed() {
  56. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  57. if (drawer.isDrawerOpen(GravityCompat.START)) {
  58. drawer.closeDrawer(GravityCompat.START);
  59. } else {
  60. super.onBackPressed();
  61. }
  62. }
  63.  
  64. @Override
  65. public boolean onCreateOptionsMenu(Menu menu) {
  66. // Inflate the menu; this adds items to the action bar if it is present.
  67. getMenuInflater().inflate(R.menu.h__admin, menu);
  68. return true;
  69. }
  70.  
  71. @Override
  72. public boolean onOptionsItemSelected(MenuItem item) {
  73. // Handle action bar item clicks here. The action bar will
  74. // automatically handle clicks on the Home/Up button, so long
  75. // as you specify a parent activity in AndroidManifest.xml.
  76. int id = item.getItemId();
  77.  
  78. //noinspection SimplifiableIfStatement
  79. if (id == R.id.action_settings) {
  80. return true;
  81. }
  82.  
  83. return super.onOptionsItemSelected(item);
  84. }
  85.  
  86. @SuppressWarnings("StatementWithEmptyBody")
  87. @Override
  88. public boolean onNavigationItemSelected(MenuItem item) {
  89. // Handle navigation view item clicks here.
  90. int id = item.getItemId();
  91.  
  92. if (id == R.id.nav_camera) {
  93. // Handle the camera action
  94. } else if (id == R.id.nav_gallery) {
  95.  
  96. } else if (id == R.id.nav_slideshow) {
  97.  
  98. } else if (id == R.id.nav_manage) {
  99.  
  100. } else if (id == R.id.nav_share) {
  101.  
  102. } else if (id == R.id.nav_send) {
  103.  
  104. }
  105.  
  106. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  107. drawer.closeDrawer(GravityCompat.START);
  108. return true;
  109. }
  110. }
  111.  
  112. package com.example.asusn46jv.sharepreference;
  113.  
  114. import android.content.Context;
  115. import android.content.Intent;
  116. import android.content.SharedPreferences;
  117. import android.support.v7.app.AppCompatActivity;
  118. import android.os.Bundle;
  119. import android.view.View;
  120. import android.widget.Button;
  121. import android.widget.TextView;
  122.  
  123. public class MainActivity extends AppCompatActivity {
  124.  
  125. Button btn_logout;
  126. TextView txt_id, txt_username;
  127. String id, username;
  128. SharedPreferences sharedpreferences;
  129.  
  130. public static final String TAG_ID = "id";
  131. public static final String TAG_USERNAME = "username";
  132.  
  133. @Override
  134. protected void onCreate(Bundle savedInstanceState) {
  135. super.onCreate(savedInstanceState);
  136. setContentView(R.layout.activity_main);
  137.  
  138. txt_id = (TextView) findViewById(R.id.txt_id);
  139. txt_username = (TextView) findViewById(R.id.txt_username);
  140. btn_logout = (Button) findViewById(R.id.btn_logout);
  141.  
  142. sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
  143.  
  144. id = getIntent().getStringExtra(TAG_ID);
  145. username = getIntent().getStringExtra(TAG_USERNAME);
  146.  
  147. txt_id.setText("ID : " + id);
  148. txt_username.setText("USERNAME : " + username);
  149.  
  150. btn_logout.setOnClickListener(new View.OnClickListener() {
  151.  
  152. @Override
  153. public void onClick(View v) {
  154. // TODO Auto-generated method stub
  155. // update login session ke FALSE dan mengosongkan nilai id dan username
  156. SharedPreferences.Editor editor = sharedpreferences.edit();
  157. editor.putBoolean(Login.session_status, false);
  158. editor.putString(TAG_ID, null);
  159. editor.putString(TAG_USERNAME, null);
  160. editor.commit();
  161.  
  162. Intent intent = new Intent(MainActivity.this, Login.class);
  163. finish();
  164. startActivity(intent);
  165. }
  166. });
  167. }
  168. }
  169.  
  170. package com.example.asusn46jv.sharepreference;
  171.  
  172. import android.app.ProgressDialog;
  173. import android.content.Context;
  174. import android.content.Intent;
  175. import android.content.SharedPreferences;
  176. import android.net.ConnectivityManager;
  177. import android.support.v7.app.AppCompatActivity;
  178. import android.os.Bundle;
  179. import android.util.Log;
  180. import android.view.View;
  181. import android.widget.Button;
  182. import android.widget.EditText;
  183. import android.widget.Toast;
  184.  
  185. import com.android.volley.Request;
  186. import com.android.volley.Response;
  187. import com.android.volley.VolleyError;
  188. import com.android.volley.toolbox.StringRequest;
  189. import com.example.asusn46jv.sharepreference.app.AppController;
  190.  
  191. import org.json.JSONException;
  192. import org.json.JSONObject;
  193.  
  194. import java.util.HashMap;
  195. import java.util.Map;
  196.  
  197. public class Login extends AppCompatActivity {
  198.  
  199.  
  200. ProgressDialog pDialog;
  201. Button btn_register, btn_login;
  202. EditText txt_username, txt_password;
  203. Intent intent;
  204.  
  205. int success;
  206. ConnectivityManager conMgr;
  207.  
  208. private String url = Server.URL + "login1.php";
  209.  
  210. private static final String TAG = Login.class.getSimpleName();
  211.  
  212. private static final String TAG_SUCCESS = "success";
  213. private static final String TAG_MESSAGE = "message";
  214.  
  215. public final static String TAG_USERNAME = "username";
  216. public final static String TAG_ID = "id";
  217.  
  218. String tag_json_obj = "json_obj_req";
  219.  
  220. SharedPreferences sharedpreferences;
  221. Boolean session = false;
  222. String id, username;
  223. public static final String my_shared_preferences = "my_shared_preferences";
  224. public static final String session_status = "session_status";
  225.  
  226.  
  227. @Override
  228. protected void onCreate(Bundle savedInstanceState) {
  229. super.onCreate(savedInstanceState);
  230. setContentView(R.layout.activity_login);
  231.  
  232. conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  233. {
  234. if (conMgr.getActiveNetworkInfo() != null
  235. && conMgr.getActiveNetworkInfo().isAvailable()
  236. && conMgr.getActiveNetworkInfo().isConnected()) {
  237. } else {
  238. Toast.makeText(getApplicationContext(), "No Internet Connection",
  239. Toast.LENGTH_LONG).show();
  240. }
  241. }
  242.  
  243. btn_login = (Button) findViewById(R.id.btn_login);
  244. btn_register = (Button) findViewById(R.id.btn_register);
  245. txt_username = (EditText) findViewById(R.id.txt_username);
  246. txt_password = (EditText) findViewById(R.id.txt_password);
  247.  
  248. // Cek session login jika TRUE maka langsung buka MainActivity
  249. sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
  250. session = sharedpreferences.getBoolean(session_status, false);
  251. id = sharedpreferences.getString(TAG_ID, null);
  252. username = sharedpreferences.getString(TAG_USERNAME, null);
  253.  
  254. if (session) {
  255. Intent intent = new Intent(Login.this, MainActivity.class);
  256. intent.putExtra(TAG_ID, id);
  257. intent.putExtra(TAG_USERNAME, username);
  258. finish();
  259. startActivity(intent);
  260. }
  261.  
  262.  
  263. btn_login.setOnClickListener(new View.OnClickListener() {
  264.  
  265. @Override
  266. public void onClick(View v) {
  267. // TODO Auto-generated method stub
  268. String username = txt_username.getText().toString();
  269. String password = txt_password.getText().toString();
  270.  
  271. // mengecek kolom yang kosong
  272. if (username.trim().length() > 0 && password.trim().length() > 0) {
  273. if (conMgr.getActiveNetworkInfo() != null
  274. && conMgr.getActiveNetworkInfo().isAvailable()
  275. && conMgr.getActiveNetworkInfo().isConnected()) {
  276. checkLogin(username, password);
  277. } else {
  278. Toast.makeText(getApplicationContext() ,"No Internet Connection", Toast.LENGTH_LONG).show();
  279. }
  280. } else {
  281. // Prompt user to enter credentials
  282. Toast.makeText(getApplicationContext() ,"Kolom tidak boleh kosong", Toast.LENGTH_LONG).show();
  283. }
  284. }
  285. });
  286.  
  287. btn_register.setOnClickListener(new View.OnClickListener() {
  288.  
  289. @Override
  290. public void onClick(View v) {
  291. // TODO Auto-generated method stub
  292. intent = new Intent(Login.this, Register.class);
  293. finish();
  294. startActivity(intent);
  295. }
  296. });
  297.  
  298. }
  299.  
  300. private void checkLogin(final String username, final String password) {
  301. pDialog = new ProgressDialog(this);
  302. pDialog.setCancelable(false);
  303. pDialog.setMessage("Logging in ...");
  304. showDialog();
  305.  
  306. StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
  307.  
  308. @Override
  309. public void onResponse(String response) {
  310. Log.e(TAG, "Login Response: " + response.toString());
  311. hideDialog();
  312.  
  313. try {
  314. JSONObject jObj = new JSONObject(response);
  315. success = jObj.getInt(TAG_SUCCESS);
  316.  
  317. // Check for error node in json
  318. if (success == 1) {
  319. String username = jObj.getString(TAG_USERNAME);
  320. String id = jObj.getString(TAG_ID);
  321.  
  322. Log.e("Successfully Login!", jObj.toString());
  323.  
  324. Toast.makeText(getApplicationContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
  325.  
  326. // menyimpan login ke session
  327. SharedPreferences.Editor editor = sharedpreferences.edit();
  328. editor.putBoolean(session_status, true);
  329. editor.putString(TAG_ID, id);
  330. editor.putString(TAG_USERNAME, username);
  331. editor.commit();
  332.  
  333. // Memanggil main activity
  334. Intent intent = new Intent(Login.this, MainActivity.class);
  335. intent.putExtra(TAG_ID, id);
  336. intent.putExtra(TAG_USERNAME, username);
  337. finish();
  338. startActivity(intent);
  339.  
  340. } else {
  341. Toast.makeText(getApplicationContext(),
  342. jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
  343.  
  344. }
  345. } catch (JSONException e) {
  346. // JSON error
  347. e.printStackTrace();
  348. }
  349.  
  350. }
  351. }, new Response.ErrorListener() {
  352.  
  353. @Override
  354. public void onErrorResponse(VolleyError error) {
  355. Log.e(TAG, "Login Error: " + error.getMessage());
  356. Toast.makeText(getApplicationContext(),
  357. error.getMessage(), Toast.LENGTH_LONG).show();
  358.  
  359. hideDialog();
  360.  
  361. }
  362. }) {
  363.  
  364. @Override
  365. protected Map<String, String> getParams() {
  366. // Posting parameters to login url
  367. Map<String, String> params = new HashMap<String, String>();
  368. params.put("username", username);
  369. params.put("password", password);
  370.  
  371. return params;
  372. }
  373.  
  374. };
  375.  
  376. // Adding request to request queue
  377. AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
  378. }
  379.  
  380. private void showDialog() {
  381. if (!pDialog.isShowing())
  382. pDialog.show();
  383. }
  384.  
  385. private void hideDialog() {
  386. if (pDialog.isShowing())
  387. pDialog.dismiss();
  388. }
  389.  
  390. }
Add Comment
Please, Sign In to add comment