Skip to content

Instantly share code, notes, and snippets.

@moea
Created January 20, 2016 11:51
Show Gist options
  • Save moea/6a910c62c9578b280bb3 to your computer and use it in GitHub Desktop.
Save moea/6a910c62c9578b280bb3 to your computer and use it in GitHub Desktop.
package io.nervous.alarmtest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});
((SeekBar)findViewById(R.id.seekBar)).setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
((TextView) findViewById(R.id.label)).setText(String.valueOf(i));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
((Button)findViewById(R.id.rtcWakeup)).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
setAlarm(AlarmManager.RTC_WAKEUP);
}
}
);
}
void setAlarm(int type) {
int minutes = ((SeekBar)findViewById(R.id.seekBar)).getProgress();
long msecs;
if(minutes == 0) {
msecs = 30 * 1000;
} else {
msecs = minutes * 60 * 1000;
}
Toast.makeText(
this, "Alarm set for " + minutes + " minutes from now", Toast.LENGTH_LONG).show();
long target;
if(type == AlarmManager.RTC_WAKEUP) {
target = System.currentTimeMillis() + msecs;
} else {
target = SystemClock.elapsedRealtime();
}
Log.w(getClass().getName(), "Expect alarm to go off at " + target + " (" + msecs + ")");
SharedPreferences prefs = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("expected", target);
editor.commit();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmWakefulReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(type, target, pending);
} else {
am.set(type, target, pending);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment