0

I have an alarmmanager onReceive event handler where I acquire wake lock.

Need help to check if Powermanager handling right:

public class PowerMgr {
private static WakeLock wlstatic=null;

synchronized public static void acquire(long timeout){
    wlstatic.acquire(timeout);
}

synchronized public static void release(){
        wlstatic.release();
}

synchronized public static void getLock(Context ctx){
    if(wlstatic==null){
        PowerManager mgr = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        wlstatic = mgr.newWakeLock(PowerManager.ON_AFTER_RELEASE|PowerManager.PARTIAL_WAKE_LOCK,"My WakeLock");
        wlstatic.setReferenceCounted(true);
    }
}
}

Following is in AlarmReceive class:


public void onReceive(Context arg0, Intent arg1) {
PowerMgr.getLock(arg0);
PowerMgr.acquire(15*1000); //I expect max 2 seconds of work. provisioned 15

String oldMsg=""; 
long nextAlarm=136570003045L; //Dummy code
String msg="newmessage"; //dummy code
if(arg1.hasExtra("ALARMMESSAGE")) {
    oldMsg = arg1.getExtras().getString("ALARMMESSAGE");
    arg1.removeExtra("ALARMMESSAGE");
}

AlarmManager am= (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(arg0, AlarmReceiver.class);
if(arg1.getExtras()== null) {
intent.putExtra("ALARMMESSAGE", (String)null);
} else {
intent.putExtra("ALARMMESSAGE", msg);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(arg0, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, nextAlarm, pendingIntent);

Intent i=new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(arg0, AlarmMessage.class);
i.putExtra("ALARMMESSAGE", oldMsg);
arg0.startActivity(i);
}

Now the big problem - When my AlarmMessage Class executes, it doesn't show DialogBox from OnCreate [Exception thrown]. When put inside OnWindowFocus, it shows up. I assumed it to be due to the Activity LifeCycle - not sure on that.

Also, if the process was not running, the AlarmMessage activity comes up and on OK click, hides itself instead of closing. When I resume it using "Recent apps" system button, the onwindowfocus event is fired again, which is not needed. Please help

AlarmMessage Class:


protected void onCreate(Bundle savedInstanceState) {
//Ringtone play
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
        try{
            wd=this.getWindow();
            ctx=wd.getContext();

            alert = new AlertDialog.Builder(ctx);
            alert.setTitle(getString(R.string.dbtable));
            Intent i = getIntent();
            if(getIntent().hasExtra("ALARMMESSAGE")) {
                alert.setMessage(i.getExtras().getString("ALARMMESSAGE"));
            }
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {if(r!=null && r.isPlaying()) r.stop(); 
                    dialog.dismiss(); finish();}
            });
            alertDialog = alert.create();
            alertDialog.show();
        }catch(Exception e){}
    }
CKmum
  • 641
  • 6
  • 17
  • The wake is working but I had to click the "power on" button in phone to show screen if the phone is sleeping - at this point system shows home screen and not the app. Due to above changes, now I cant see the message ! Really confused here... Help needed – CKmum May 05 '13 at 20:39
  • used: android:excludeFromRecents="true" – CKmum May 07 '13 at 11:22

0 Answers0