2013-06-19 124 views
0

我有一些问题,警报对话框...我怎么可以调用alert.show();在第二类中调用警报对话框?如何在另一个类中使用方法中的变量?

我需要显示alertdialog的onReceive方法,但我不能这样做......

有人可以帮我吗?

p.s.对不起,我的英语..; |

主类:

public class Main extends Activity { 

    ... 

    public void onTimeSet(TimePicker view, int hour, int minute) { 

    ... 


       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("ALARM"); 
       builder.setMessage("Wstajesz czy dalej drzemiesz ?!"); 

       builder.setPositiveButton("Wstaje...", new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         // Do do my action here 

         dialog.dismiss(); 
        } 

       }); 

       builder.setNegativeButton("Spie!", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // I do not need any action here you might 
         dialog.dismiss(); 
        } 
       }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
       .... 
    } 

二等:

public class Second extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context k1, Intent k2) { 

     /* 
      --> here i want to call an alert using: alert.show(); It's possible ? 

     */ 
     } 
} 

回答

0

您不能显示从Receiver一个Dialog,你需要一个Activity。请使用Intent开始您的MainActivity或根据您的要求,将其作为单独的ActivityDialog theme。您可以通过创建一个Activity,并用主题宣告它在你的manifest

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

需要注意的是,你需要从Receiver创建Intent何时开始的Activity

设置 Intent.FLAG_ACTIVITY_NEW_TASK标志做到这一点
相关问题