Reputation:
I am showing Alert Dailog Box through Service.
ShowAlert.java
public class ShowAlert extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setTitle("Alert Title")
.setMessage("Alert Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
Inside my service onStartCommand I have a timer thread which runs every 30s. I am starting the activity inside the Timer.
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(this,ShowAlert.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}, 1000, 30000);
In my android manifest I used the Dialog theme.
<activity android:name=".ShowAlert"
android:theme="@android:style/Theme.Dialog"></activity>
I got Dailog Box as below.
On click of "OK" I see another Dialog Box with me app name as follows
Can anyone help me. I don't understand why I see second dialog box.
Upvotes: 0
Views: 802
Reputation: 1412
Add finish()
when you want to dismiss the alert dialog.
public class ShowAlert extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setTitle("Alert Title")
.setMessage("Alert Message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Insert this new Code
alertDialog.dismiss(); //dismiss the alertdialog
yourActivity.this.finish(); //Destroy the Activity
}
}).show();
}
}
Upvotes: 0
Reputation: 9870
The Second DialogBox is not a real Dialog, it is Your Activity because You set the Theme inside the manifest as Theme.Dialog:
<activity android:name=".ShowAlert"
android:theme="@android:style/Theme.Dialog"></activity>
So what happens is, You start the Activity from intent at the service and by starting the Activity, Your AlertDialog that You have created inside Your Activity starts also. By canceling the dialog, the AlertDialog disappears and Your Activity is shown. You havent set any content view, so the Activity with the size of a small dialog is shown, only with the app name.
So to get rid of this, don´t use
android:theme="@android:style/Theme.Dialog"
What´s Your Intention behind showing the Activity as Dialog? Do You only want to show the alertDialog? Then You can either make a xml layout that looks like You want and setContentView(R.layout.yourLayout) inside Your Activity, or You can set a blank layout and show only the AlertDialog.
Upvotes: 2