0

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>

1 Answers1

0

If the phone is alseep, your phone will only wake up for the time to execute Broadcast receiver. It might or might not remain wake when the activity executes.

To solve this, you should acquire the wake lock in the Receiver itself and then start the activity...

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
               PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, App.TAG);

                wl.acquire();  
Log.i("Alarm recieved", "Executing on receive");
                Intent i = new Intent();
                i.setClassName("com.example.attendogram", "com.example.attendogram.Reminder");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg0.startActivity(i);
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • Hi, This code doesn't even turn my emulator on like it did when I used the wake lock in the activity I started. The log cat shows all the code in the broadcast and receiving activity are being executed. Is there something I'm missing (I've added the required permission) – Haran Sivaram Jul 15 '14 at 08:11
  • The above flag itself doesn't wake up the screen. I have to use getwindow().addflags() to wake the screen up. But my 'DISMISS_KEYGAURD' is still not working. Will posting my classes and manifest help you understand better? – Haran Sivaram Jul 15 '14 at 10:18
  • Yes, send me the project if possible – Madhur Ahuja Jul 15 '14 at 10:23
  • I've added the relevant classes and my manifest to the earlier post. Please do check out, haven't changed much since last time. – Haran Sivaram Jul 15 '14 at 12:17