2

當android中的鬧鐘響起時,我想創建一個AlertDialog。另外,我想創建一個通知,具體取決於用戶在對話框的單選按鈕中單擊的選項。 當我嘗試使用contextgetApplicationContext()時出現問題。在課堂上使用上下文擴展BroadcastReceiver

這是我的代碼:

public void onReceive(final Context context, Intent intent) 
{ 
    final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "}; 
    String doseName = intent.getStringExtra("doseName"); 
    Toast.makeText(context, "Take medicine: " + doseName, Toast.LENGTH_LONG).show(); 

    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle("It's time for your medicine."); 
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      switch(item) 
      { 
      case 0: 
       Toast.makeText(context, "Good.", Toast.LENGTH_SHORT).show(); 
       break; 
      case 1: 
       Toast.makeText(context, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show(); 
       break; 
      case 2: 
       Intent service1 = new Intent(context, DoseAlarmService.class); 
       service1.putExtra("doseName", doseName); 
       context.startService(service1); 
       break; 
      } 
     } 
    }); 
    levelDialog = builder.create(); 
    levelDialog.show(); 
} 

我一直在使用getApplicationContext代替context開關盒內試過了,但是這是確切的錯誤我得到:

The method getApplicationContext() is undefined for the type new DialogInterface.OnClickListener(){} 

如何任何建議前進?

編輯:

截至目前,這些是我已經試過:

public void onReceive(final Context context, Intent intent) 
{ 
     ctx = context; 
    final CharSequence[] items = {" I'm taking the dose now! "," Remind again in ten minutes. "," Ignore for now. "}; 
    String doseName = intent.getStringExtra("doseName"); 
    Toast.makeText(ctx, "Take medicine: " + doseName, Toast.LENGTH_LONG).show(); 

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx.getApplicationContext()); 
    builder.setTitle("It's time for your medicine."); 
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      switch(item) 
      { 
      case 0: 
       Toast.makeText(ctx, "Good.", Toast.LENGTH_SHORT).show(); 
       break; 
      case 1: 
       Toast.makeText(ctx, "Reminder set in ten minutes.", Toast.LENGTH_SHORT).show(); 
       break; 
      case 2: 
       Intent service1 = new Intent(ctx.getApplicationContext(), DoseAlarmService.class); 
       service1.putExtra("doseName", doseName); 
       ctx.startService(service1); 
       break; 
      } 
     } 
    }); 
    levelDialog = builder.create(); 
    levelDialog.show(); 
} 

而且,而是採用ctx,我心中已經直接使用context.getApplicationContext()和檢查。它不起作用。

此外,當我註釋掉所有有問題的領域,只是運行,以驗證對話框輪番上漲,我得到這個異常:

07-23 13:26:21.316: E/AndroidRuntime(1756): java.lang.RuntimeException: Unable to start receiver com.dosemanager.ui.DoseAlarmReceiever: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

請幫幫忙!

+1

你可以使用context.getApplicationContext()。 –

+0

現在,這是另一個問題。沒有活動就無法顯示對話框。創建新的活動並在那裏顯示對話框。 –

+0

嘗試使用'Activity cxt =(Activity)上下文;'並使用cxt作爲你的上下文 –

回答

3

嗯,你已經有你的背景 - 這是onReceive()的參數。你不需要使用getApplicationContext()

編輯:不能使用開關的情況下背景下,因爲背景是在接收器類中定義你想在onClickListener類使用它。 我的建議是:

public class %YOUR_RECEIVER_CLASS% { 
     private Context context; 
     public onReceive(Context context, ...) { 
      this.context = context; 
     } 
    } 

現在你可以使用上下文到處

+0

試過了......它不工作。 –

+0

他可以在onClickListener中使用上下文,因爲他的狀態是'最終' –

+0

LOL no。它是否是最終的,但它只是方法的一個參數,而不是類的成員。 –

0

使用getBaseContext()而不是getApplicationContext()

例如廣播接收器::

@Override 
    public void onCreate() 
    {   
     Toast.makeText(this, "SmsSenderService()", Toast.LENGTH_LONG).show(); 

     sendBroadcastReceiver = new BroadcastReceiver() 
     { 
       public void onReceive(Context arg0, Intent intent) 
       { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK:     

        Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();   

        break; 

       } 
      } 
     }; 
+0

我剛剛試過...沒有工作。 –

+0

哦,親愛的上帝,你正試圖在ONCLICKLISTENER中激發getBaseContext方法。 OnClickListener沒有這種方法。您需要將上下文推送到您的onClickListener,或者在您的Receiver AS I SAID中使Context成爲一個字段。我需要重複多少次?不要聽誰的建議使用getContext或getBaseContext或其他東西,因爲他們不知道 –