i created a program that will ring an alarm if it received a message even when phone is locked. when the phone rings, it will show dismiss button to stop the alarm, but my code just shows the dismiss button and disappears quickly leaving me with no option on how to stop the alarm. please help me with this code. thanks for help in advance
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiverinterface);
unlockScreen();
alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Alarm");
alertDialogBuilder
.setMessage("Stop Alarm")
.setCancelable(false)
.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Ringtone r = EAlarmReceiver.r;
r.stop();
Toast.makeText(context.getApplicationContext(), "Alarm Stopped", Toast.LENGTH_LONG).show();
if(EAlarmReceiver.sms.length() > 10)
{
Intent openInterface = new Intent("proj.receiver.VIEWMESSAGE");
openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInterface);
}
else
{
Intent openInterface = new Intent("proj.receiver.RECEIVERINTERFACE");
openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInterface);
}
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}// end oncreate()
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
//unlock screen
public void unlockScreen() {
//make the activity show even the screen is locked.
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+ WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
Try this code after removing
finish()from onPause(). post complete code too.Edit :- As you said removing finish worked for you, here is solution of your second question.
1) Complex and temporary solution :- try to add a boolean variable, which u should set to true on action taken on AlertDialog, and if it is true in onResume , finish ur activity. OnResume calls each time u comes back to ur activity.
2) But if i were at ur place, i would have followed below approach.
Create a layout looks like alert Dialog. and setup Activity theme as Dialog in activity declaration in manifest, it will open ur activity as dialog. hence u need not to create an AlertDialog to handle action, u can add buttons for dismiss and all.