I have an alarm that sets off after a certain time using an activity. This Activity shows up under WAKE_LOCK function. Now, I want the activity to display information of the alarm. I use TextViews to display the information inside the onCreate of the Activity. And for some reason this test code doesn't work.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
TextView tvName = (TextView) findViewById(R.id.alarm_screen_name);
tvName.setText("123");
Button dismissButton = (Button) findViewById(R.id.reminderDismiss);
dismissButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//Ensure wakelock release
Runnable releaseWakelock = new Runnable() {
@Override
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
finish();
}
}
};
new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}
Do you guys have any idea why it's not working?
activity_screen XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.ScreenActivity">
<TextView
android:id="@+id/alarm_screen_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Alarm!"
android:textSize="38dp" />
<TextView
android:id="@+id/alarm_screen_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="00 : 00"
android:textSize="52dp" />
<TextView
android:id="@+id/alarm_screen_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Alarm name" />
<Button
android:id="@+id/reminderDismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="106dp"
android:text="Dismiss"
android:onClick="dismiss"
android:textSize="38dp" />
The Activity Shows up From this WAKE_LOCK:
public class AlarmService extends Activity {
private PowerManager.WakeLock mWakeLock;
public void onCreate(Bundle savedInstateState) {
super.onCreate(savedInstateState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Wake Log");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_screen);
//stop alarm
Button stopAlarm = (Button) findViewById(R.id.reminderDismiss);
stopAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
And the service activates under this button from another activity:
public void saveAlarm(View v) {
//set time into calendar instance
GregorianCalendar calendar= new GregorianCalendar();
calendar.set(GregorianCalendar.HOUR_OF_DAY,reminderHour);
calendar.set(GregorianCalendar.MINUTE,reminderMinute);
calendar.set(GregorianCalendar.SECOND,00);
calendar.set(GregorianCalendar.MILLISECOND,0000);
AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent reminderintent = new Intent(this, AlarmService.class);
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);
finish();
}