I have read through a lot of answers regarding this and still cannot figure out a way to do it.
I have an app which sets a reminder for a meeting and shows a pop up activity 15 minutes before the appointment and getting an input from the user about the appointment.
I use an AlarmManager to set the alarm, and start an activity from the broadcast reciever. Works fine when the device is on, but if it is a sleep it doesn't turn on my phone (However I do manage to get the lock screen on, on my emulator). In either case I cannot unlock the phone and display my activity without the user unlock his phone manually.
My main activity
public class Start extends Activity {
public static int no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
System.out.println("On create. After content set.");
LinearLayout l = (LinearLayout) findViewById(R.id.startlayout);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, new GregorianCalendar().getTimeInMillis()+10000, PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReciever.class), Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
My broadcast reveiver
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Log.i("Alarm recieved", "Executing on receive");
PowerManager pm = (PowerManager)arg0.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Tag");
wl.acquire();
Intent i = new Intent();
i.setClassName("com.example.attendogram", "com.example.attendogram.Reminder");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);
Log.i("Alarm recieved", "Executed on receive");
}
}
Activity started by broadcast receiver
public class Reminder extends Activity {
private Context context;
public Reminder() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("Reminder created", "Executing onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Log.i("Reminder", "Screen tunred on");
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Log.i("Reminder", "Dismissed");
setFinishOnTouchOutside(false);
TextView rem = (TextView) findViewById(R.id.reminder_sna);
rem.setText(getIntent().getStringExtra("id"));
context = this;
}}
And my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.attendogram"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.attendogram.Start"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SubPage"></activity>
<activity android:name="NewSub"></activity>
<activity android:name="Timetable" >
</activity>
<receiver android:name=".AlarmReciever" />
<activity
android:name="Reminder"
android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar.MinWidth"
android:excludeFromRecents="true" >
</activity>
</application>
</manifest>