2011-07-22 21 views
0

我有一個C2DM的工作實現,如果我處理每一條收到Toast的消息。C2DM在收到時顯示AlertDialog

取而代之的是,我想使用一個警報對話框,用戶說可以確認該消息。

我爲此編寫的代碼在show()行生成一個異常。我期望這個問題可能與所使用的上下文(?)有關。

AlertDialog.Builder alertbox = new AlertDialog.Builder(context); 
alertbox.setMessage("Received Push Notification"); 
alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface arg0, int arg1) { 
    // TODO Auto-generated method stub 

    } }); 
    AlertDialog alert = alertbox.create(); 
    alert.show(); 

全類(如評論請求):

public class MyC2DM 

extends BroadcastReceiver { 
    public MyC2DM() 
    { 


    } 

    @Override 
    public void onReceive(Context context, Intent intent) 
     { 
      Log.i("Recieve","2"); 
      //Toast.makeText(context, "Intent Receive!", Toast.LENGTH_SHORT).show(); 
      if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) 
      { 
       handleRegistration(context, intent); 
      } 
      else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) 
      { 
       handleMessage(context, intent); 
      } 
     } 

     private void handleRegistration(Context context, Intent intent) 
     { 

      String registration = intent.getStringExtra("registration_id"); 
      if (intent.getStringExtra("error") != null) 
      { 
       Toast.makeText(context, "Reg Error!", Toast.LENGTH_LONG).show(); 
       // Registration failed, should try again later. 
      } 
      else if (intent.getStringExtra("unregistered") != null) 
      { 
       // unregistration done, new messages from the authorized sender will be rejected 
       Toast.makeText(context, "Unreg!", Toast.LENGTH_LONG).show(); 
      } 
      else if (registration != null) 
      { 
       // Send the registration ID to the 3rd party site that is sending the messages. 
       // This should be done in a separate thread. 
       // When done, remember that all registration is done. 

      // UserId = customer.getId(); 
      // Log.i("id",String.valueOf(UserId)); 

       String RegId = registration; 
       Log.i("reg",String.valueOf(RegId)); 
      } 
     } 

     private void handleMessage(Context context, Intent intent) 
     { 
      // Message handler. 
      Log.i("Recieve","MESSAGE"); 

      AlertDialog.Builder alertbox = new AlertDialog.Builder(context); 
      alertbox.setMessage("Received Push Notification"); 

      alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface arg0, int arg1) { 
        // TODO Auto-generated method stub 

       } }); 
      AlertDialog alert = alertbox.create(); 
      alert.show(); 

      /*Toast.makeText(context, "ALERT: " + intent.getStringExtra("payload"), Toast.LENGTH_LONG).show();*/ 

     } 
} 
+0

顯示更多code.Where做你把這些code.Its確定的,如果你的背景有沒有問題 – Rasel

+0

這是在'公共類MyC2DM擴展BroadcastReceiver'內'私人無效的handleMessage(上下文的背景下,意圖意圖)'。 – mctedo

+0

已添加全班提交。 – mctedo

回答

0

有沒有辦法從廣播接收器和Service.Use通知,而不是

0

顯示對話框中,它是不可能顯示一個對話框在BroadcastReceiver中。我有同樣的問題。我創建了一個Activity,它從android:style/Theme.Dialog繼承它的風格。這使得活動看起來像一個對話框。