Created
March 20, 2014 00:11
-
-
Save jacksonfdam/9654568 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.alarmmanager; | |
import android.app.Activity; | |
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.os.SystemClock; | |
import android.view.Menu; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
final static private long UM_SEGUNDO = 1000; | |
final static private long VINTE_SEGUNDOS = UM_SEGUNDO * 20; | |
PendingIntent pi; | |
BroadcastReceiver br; | |
AlarmManager am; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setup(); | |
findViewById(R.id.the_button).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, | |
SystemClock.elapsedRealtime() + VINTE_SEGUNDOS, pi); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
private void setup() { | |
br = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context c, Intent i) { | |
Toast.makeText(c, "Tarefa Executada", Toast.LENGTH_LONG).show(); | |
} | |
}; | |
registerReceiver(br, new IntentFilter("com.example.alarmmanager")); | |
pi = PendingIntent.getBroadcast(this, 0, new Intent( | |
"com.example.alarmmanager"), 0); | |
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE)); | |
} | |
@Override | |
protected void onDestroy() { | |
am.cancel(pi); | |
unregisterReceiver(br); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment