0

我有一个警报管理器 onReceive 事件处理程序,我在其中获取唤醒锁。

需要帮助检查 Powermanager 处理是否正确:

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);
    }
}
}

以下是在 AlarmReceive 类中:


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);
}

现在的大问题 - 当我的 AlarmMessage 类执行时,它不显示来自 OnCreate [抛出异常] 的 DialogBox。当放入 OnWindowFocus 时,它会显示出来。我认为这是由于 Activity LifeCycle - 不确定。

此外,如果进程没有运行,AlarmMessage 活动会出现并单击“确定”,隐藏自身而不是关闭。当我使用“最近的应用程序”系统按钮恢复它时,再次触发 onwindowfocus 事件,这是不需要的。请帮忙

报警消息类:


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){}
    }
4

0 回答 0