I apologies in advance if I'm not good in writing English. I'm writing a simple task app that remind me with alarm in specific time.
Below I set alarm with AlarmManager :
private static void setAlarm(Context context, Calendar calendar,
PendingIntent pIntent) {
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pIntent);
}
}
and then AlarmManagerHelper :
public class AlarmManagerHelper extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String title = intent.getStringExtra("Title");
int hour = intent.getIntExtra("Hour", 0);
int min = intent.getIntExtra("Minute", 0);
String alarmTone = intent.getStringExtra("AlarmTone");
Intent i = new Intent();
i.setClassName("com.example.tasks",
"com.example.tasks.AlarmScreenActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("Title", title);
i.putExtra("Hour", hour);
i.putExtra("Minute", min);
i.putExtra("AlarmTone", alarmTone);
context.startActivity(i);
}
}
and AlarmScreenActivity is:
public class AlarmScreenActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get intent
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
mPlayer = new MediaPlayer();
try {
if (task_Tone != null && !task_Tone.equals("")) {
android.net.Uri toneUri = android.net.Uri.parse(task_Tone);
if (toneUri != null) {
mPlayer.setDataSource(this, toneUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.setLooping(true);
mPlayer.prepare();
mPlayer.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// some code
public void onClickDissmis(View view) {
mPlayer.stop();
finish();
}
protected void onDestroy() {
super.onDestroy();
wl.release();
}
}
then with AlarmManagerHelper and AlarmScreenActivity displaying it.
my problem is: in the specific time that should wake up and ringing not do int, so when I press power button an turn screen on that is work???! (when is in debug mode and the device , connected to system work properly) I hope that describe my problem perfectly.