Android在主屏幕上显示Dialog

时间:2013-05-24 20:57:28

标签: android

我想在10秒后进入主屏幕时显示Dialog,

我将主题修改为Theme.Dialog,

现在,当我将Back Key按回主屏幕时,我成功弹出了它。

但我的问题是,在我按下Home Key并进入主屏幕后,

10秒后

对话框不会弹出

但是,当我打开我的应用程序时,

我发现它已经出现在那里。

那么,有人能告诉我如何解决这个问题吗?

这是我的代码(我使用了两个活动):

第一项活动:

public class MainActivity extends Activity {    
private Button bt_dialog;
AlertDialog.Builder builder;
AlertDialog mDialog;
Intent i = new Intent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt_dialog = (Button) findViewById(R.id.button1);
    i.setClass(MainActivity.this, DialogActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
            //click button and show the dialog after 10s
    bt_dialog.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(final View view) {
        CountDownTimer dlgCountDown;
        dlgCountDown = new CountDownTimer(10000, 1000){
        public void onTick(long millisUntilFinished) { 
        } 
        public void onFinish(){
            startActivity(i); 
        } 
        }.start(); 
        }
    });
}

第二项活动:

public class DialogActivity extends Activity{   
AlertDialog.Builder builder;
AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog);   
    String dialogText = "dialog text";
        TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setText(dialogText);
        Button dismissbutton = (Button) findViewById(R.id.button1);
        dismissbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            DialogActivity.this.finish();
      }
    });
  }

这是我的清单代码:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testdialog.MainActivity"
        android:theme="@android:style/Theme"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity 
        android:name="com.example.testdialog.DialogActivity"
        android:theme="@android:style/Theme.Dialog">
    </activity>

2 个答案:

答案 0 :(得分:0)

您的代码运行正常。 如果我没有弄错你的问题是Button上的事件无效。 从活动2返回活动1

我刚刚在您的代码中的不同位置添加了Log,以便您可以了解正在发生的事情以及在哪个位置。

MainActivity

public class MainActivity extends Activity {
private Button bt_dialog;
AlertDialog.Builder builder;
AlertDialog mDialog;
Intent i = new Intent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt_dialog = (Button) findViewById(R.id.button1);
    i.setClass(Experiment.this, DialogActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // click button and show the dialog after 10s
    Log.i("MainActivity ", "Create");
    bt_dialog.setOnClickListener(new Button.OnClickListener() {

        public void onClick(final View view) {
            Log.i("MainActivity ", "onClick--Start");
            CountDownTimer dlgCountDown;
            dlgCountDown = new CountDownTimer(10000, 1000) {
                public void onTick(long millisUntilFinished) {
                }

                public void onFinish() {
                    Log.i("MainActivity ment", "onClick-- Finish");
                    startActivity(i);
                }
            }.start();
        }

    });
   }
}

第二项活动

public class DialogActivity extends Activity {
AlertDialog.Builder builder;
AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_dialog);
    String dialogText = "dialog text";
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(dialogText);
    Log.i("Dialog", "onCreate");
    Button dismissbutton = (Button) findViewById(R.id.button1);
    dismissbutton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            Log.i("Dialog", "onClick");
            DialogActivity.this.finish();
        }
    });
   }
}

记录Cat输出

05-25 10:33:14.007: I/dalvikvm-heap(19907): Grow heap (frag case) to 16.102MB for 3821200-byte allocation
05-25 10:33:14.117: I/dalvikvm-heap(19907): Grow heap (frag case) to 22.580MB for 6793232-byte allocation
05-25 10:33:14.267: I/MainActivity(19907): Create
05-25 10:33:19.267: I/MainActivity(19907): onClick--Start
05-25 10:33:29.267: I/MainActivity(19907): onClick-- Finish
05-25 10:33:29.337: I/Dialog(19907): onCreate
05-25 10:33:34.007: I/Dialog(19907): onClick
05-25 10:33:41.288: I/MainActivity(19907): onClick--Start
05-25 10:33:51.297: I/MainActivity(19907): onClick-- Finish
05-25 10:33:51.357: I/Dialog(19907): onCreate

因此,Button中的MainActivity似乎正常运作

答案 1 :(得分:0)

我只是通过在清单文件中添加它来解决它:

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

在应用程序标记中添加。