在Dialog中显示应用程序名称

时间:2014-11-14 14:26:36

标签: android timer dialog android-service

我正在通过服务显示Alert Dailog Box。

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

在我的服务onStartCommand中,我有一个每隔30秒运行一次的计时器线程。我正在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);

在我的Android清单中,我使用了Dialog主题。

<activity android:name=".ShowAlert"
android:theme="@android:style/Theme.Dialog"></activity>

我的Dailog Box如下所示。

Image 1

点击&#34;确定&#34;我看到另一个带有我的应用程序名称的对话框如下

Image 2

任何人都可以帮助我。我不明白为什么我会看到第二个对话框。

2 个答案:

答案 0 :(得分:2)

第二个DialogBox不是真正的Dialog,它是你的Activity,因为你将清单中的主题设置为Theme.Dialog:

    <activity android:name=".ShowAlert"
    android:theme="@android:style/Theme.Dialog"></activity>

所以会发生什么,你从服务意图启动Activity,并启动Activity,你在Activity中创建的AlertDialog也开始了。 通过取消对话框,AlertDialog将消失,并显示您的活动。您没有设置任何内容视图,因此只显示一个小对话框大小的活动,只显示应用名称。

所以要摆脱这个,不要使用

     android:theme="@android:style/Theme.Dialog"

将您的活动显示为对话背后的意图是什么?你只想显示alertDialog吗?然后,您可以在“活动”中创建一个看起来像你想要的xml布局和setContentView(R.layout.yourLayout),或者你可以设置一个空白布局并只显示AlertDialog。

答案 1 :(得分:0)

如果要关闭提醒对话框,请添加finish()

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