2013-02-26 97 views
0

我想通過傳遞一個方法(postMessage)broadcastReceiver傳遞上下文,但不工作。無法通過broadcastReceiver調用方法

什麼是錯誤?我嘗試了很多東西,但仍然沒有工作。

public class AlarmReceiver extends BroadcastReceiver implements 
      OnActivityResultListener { 


     public void onReceive(Context context, Intent intent) { 
      try { 
     controler(context); 

      } catch (Exception e) { 
       Toast.makeText(
         context, 
         "There was an error somewhere", 
         Toast.LENGTH_SHORT).show(); 
       e.printStackTrace(); 
      } 

     } 

    public void controler(Context context) { 
    String radioButtonName = MAinActivity.actionAlarmName(radioButtonName); 

      if (radioButtonName.equals("1")) { 
     //  TODO 
      } else if (radioButtonName.equals("2")) { 
    postMessage(context); 
    } 
    } 

    public void postMessage(Context context) { 

      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which) { 
        case DialogInterface.BUTTON_POSITIVE: 
         // Yes button clicked 

         break; 

        case DialogInterface.BUTTON_NEGATIVE: 
         // No button clicked 
         break; 
        } 
       } 
      }; 
      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setMessage("Are you sure?") 
        .setPositiveButton("Yes", dialogClickListener) 
        .setNegativeButton("No", dialogClickListener).show(); 
     } 

回答

1

如果王某沒有表現出活動場景中接收一個對話框,您可以在此:

dialog.getWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)

不要忘記在AndroidManifest添加權限。 XML。

+0

是的,它的工作原理,謝謝。 – user1207965 2013-03-02 03:40:53

1

在AndroidManifest
添加您的接收器

<receiver android:name=".AlarmReceiver" android:process=":remote" /> 
+0

我已經將接收器添加到清單文件,但仍然無法工作。 – user1207965 2013-02-26 03:47:26

+0

分享你的方法來設置鬧鐘。 – ihsanhf 2013-02-26 05:06:09

2

Context您在BroadcastReceiveronReceive()接收不活動上下文,它的應用環境。您不能使用應用程序上下文顯示對話框。對話框應始終與Activity關聯。