I work for alarm manager now. i get a good tutorial http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/
this is my modified code :
public class Main extends Activity implements OnClickListener{
final static private long ONE_SECOND = 1000;
final static private long TWENTY_SECONDS = ONE_SECOND * 20;
final static private long FIVE_SECONDS = ONE_SECOND * 5;
final static private long TEN_SECONDS = ONE_SECOND * 10;
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setup();
findViewById(R.id.the_button).setOnClickListener(this);
}
private void setup() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
}
};
registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey") );
pi = PendingIntent.getBroadcast( this, 0, new Intent("com.authorwjf.wakeywakey"), 0 );
am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
@Override
public void onClick(View v) {
am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + FIVE_SECONDS, pi );
}
@Override
protected void onDestroy() {
am.cancel(pi);
unregisterReceiver(br);
super.onDestroy();
}
}
but i have a problem now, when i close the apps, alarm not works,
How to make this apps still ring alarm when it close or not active?
0 comments:
Post a Comment