Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. private EditText username,password;
  4. public static final String MyPREFERENCES = "MyPrefs" ;
  5. public static final String name = "nameKey";
  6. public static final String pass = "passwordKey";
  7. SharedPreferences sharedpreferences;
  8.  
  9. public static Long t0;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. username = (EditText)findViewById(R.id.editText1);
  16. password = (EditText)findViewById(R.id.editText2);
  17.  
  18. username.setText("***");
  19. password.setText("***");
  20.  
  21. t0 = Welcome.t0;
  22. }
  23.  
  24.  
  25. @Override
  26. protected void onResume() {
  27. sharedpreferences=getSharedPreferences(MyPREFERENCES,
  28. Context.MODE_PRIVATE);
  29. if (sharedpreferences.contains(name))
  30. {
  31. if(sharedpreferences.contains(pass)){
  32. Intent i = new Intent(this,SecondActivity.class);
  33. startActivity(i);
  34. }
  35.  
  36. Calendar c = Calendar.getInstance();
  37. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  38. String strDate = sdf.format(c.getTime());
  39. System.out.println(strDate);
  40.  
  41. Date d;
  42. try {
  43. d = sdf.parse(strDate);
  44. long second2=d.getSeconds();
  45.  
  46. long ts=Math.abs(second2-t0);
  47.  
  48. if (ts>=10){
  49.  
  50. logout();
  51. Intent i2 = new Intent(this,MainActivity.class);
  52. startActivity(i2);
  53. }
  54.  
  55. } catch (ParseException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59.  
  60. }
  61. super.onResume();
  62. }
  63.  
  64. public void login(View view){
  65. Editor editor = sharedpreferences.edit();
  66. String u = username.getText().toString();
  67. String p = password.getText().toString();
  68. if(u.equals("***") && p.equals("***")){
  69.  
  70. editor.putString(name, u);
  71. editor.putString(pass, p);
  72. editor.commit();
  73. Intent i = new Intent(this,SecondActivity.Welcome.class);
  74. startActivity(i);
  75. }
  76. else{
  77. Toast.makeText(MainActivity.this, "ko", Toast.LENGTH_SHORT).show();
  78.  
  79. }
  80. }
  81. @Override
  82. public boolean onCreateOptionsMenu(Menu menu) {
  83. // Inflate the menu; this adds items to the action bar if it is present.
  84. getMenuInflater().inflate(R.menu.main, menu);
  85. return true;
  86. }
  87.  
  88.  
  89. public void onPause() {
  90. super.onPause();
  91. this.finish();
  92. }
  93.  
  94. public void closing() {
  95. finish();
  96. }
  97.  
  98. public void logout(){
  99. SharedPreferences sharedpreferences = getSharedPreferences
  100. (MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
  101. Editor editor = sharedpreferences.edit();
  102.  
  103. editor.clear();
  104. editor.commit();
  105. moveTaskToBack(true);
  106. this.finish();
  107. }
  108.  
  109. }
  110.  
  111. public class Second extends Activity {
  112.  
  113. static Long t0;
  114.  
  115. @Override
  116. protected void onCreate(Bundle savedInstanceState) {
  117. super.onCreate(savedInstanceState);
  118. setContentView(R.layout.second_welcome);
  119.  
  120. }
  121.  
  122. @Override
  123. public boolean onCreateOptionsMenu(Menu menu) {
  124. // Inflate the menu; this adds items to the action bar if it is present.
  125. getMenuInflater().inflate(R.menu.welcome, menu);
  126. return true;
  127. }
  128. public void logout(View view){
  129. SharedPreferences sharedpreferences = getSharedPreferences
  130. (MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
  131. Editor editor = sharedpreferences.edit();
  132.  
  133. editor.clear();
  134. editor.commit();
  135. moveTaskToBack(true);
  136. this.finish();
  137. }
  138.  
  139. public void exit(View view){
  140.  
  141. moveTaskToBack(true);
  142. this.finish();
  143.  
  144. }
  145.  
  146. public void onPause() {
  147. super.onPause();
  148. Calendar c = Calendar.getInstance();
  149. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  150. String strDate = sdf.format(c.getTime());
  151.  
  152.  
  153. Date d;
  154. try {
  155. d = sdf.parse(strDate);
  156. long second=d.getSeconds();
  157.  
  158. Welcome.t0=second;
  159.  
  160. } catch (ParseException e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. }
  164.  
  165. this.finish();
  166.  
  167. }
  168.  
  169. public void closing() {
  170. finish();
  171. }
  172.  
  173. }
  174.  
  175. Calendar cal = Calendar.getInstance();
  176.  
  177. cal.add(Calendar.MINUTE, "YourTimeinMinutes"); // you can use Calendar.Seconds etc according to your need
  178.  
  179. Intent intent = new Intent(YourActivity.this, AlarmReceiver.class);
  180. PendingIntent sender = PendingIntent.getBroadcast(YourActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  181.  
  182. // Get the AlarmManager service
  183. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  184. am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
  185.  
  186. public class AlarmReceiver extends BroadcastReceiver
  187. {
  188. @Override
  189. public void onReceive(Context context, Intent intent)
  190. {
  191. //your logic
  192. }
  193. }
  194.  
  195. <receiver android:name=".AlarmReceiver" ></receiver>
  196.  
  197. new CountDownTimer(10000, 1000) {//CountDownTimer for 10 seconds
  198.  
  199. public void onTick(long millisUntilFinished) {
  200. }
  201.  
  202. public void onFinish() {
  203. //logout here.
  204. SharedPreferences sharedpreferences = getSharedPreferences
  205. (MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
  206. Editor editor = sharedpreferences.edit();
  207.  
  208. editor.clear();
  209. editor.commit();
  210. moveTaskToBack(true);
  211. this.finish();
  212. }
  213. }
  214. .start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement