2013-06-19 49 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標誌做到這一點
相關問題