daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.vardaan.buttonenable;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13.  
  14. import java.util.Calendar;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18.     Button clickButton;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.  
  25.         clickButton = findViewById(R.id.clickButton);
  26.  
  27.         Calendar calendar = Calendar.getInstance();
  28.         calendar.set(Calendar.HOUR_OF_DAY, 21);
  29.         calendar.set(Calendar.MINUTE, 41);
  30.         calendar.set(Calendar.SECOND, 0);
  31.  
  32.         Intent intent = new Intent(this, Receive.class);
  33.         PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  34.  
  35.         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  36.         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
  37.  
  38.  
  39.         final SharedPreferences sharedPreferences = getSharedPreferences("buttonPref", Context.MODE_PRIVATE);
  40.         boolean buttonTrigger = sharedPreferences.getBoolean("buttonTrigger", true);
  41.         if (!buttonTrigger) {
  42.             clickButton.setEnabled(false);
  43.         }
  44.  
  45.  
  46.         clickButton.setOnClickListener(new View.OnClickListener() {
  47.             @Override
  48.             public void onClick(View v) {
  49.  
  50.                 Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
  51.                 clickButton.setEnabled(false);
  52.  
  53.                 SharedPreferences.Editor editor = sharedPreferences.edit();
  54.                 editor.putBoolean("buttonTrigger", false);
  55.                 editor.apply();
  56.  
  57.             }
  58.         });
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top