2012-01-07 23 views
1

無論何時監聽器收到消息,我都希望將進度和警報對話框添加到活動上下文中。有了這個代碼:來自c2dm偵聽器事件的Tigger alertBox

public class C2DMMessageReceiver extends BroadcastReceiver { 
     @Override 
       public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction(); 
        Log.w("C2DM", "Message Receiver called"); 
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { 
         Log.w("C2DM", "Received message"); 
         final String payload = intent.getStringExtra("payload"); 
         Log.d("C2DM", "dmControl: payload = " + payload); 

         // Message handling 
         if(payload.equals("DataUpdate")) { 
          progressDialog = ProgressDialog.show(context, "Please wait...", "Synchronizing data ...", true); 
       syncData(context); 
       progressDialog.dismiss(); 
       AlertDialog.Builder alertbox = new AlertDialog.Builder(context); 
       alertbox.setMessage("Data was updated"); 
       alertbox.create(); 
       alertbox.show(); 
         } 
        } 
       } 
    } 

我收到以下錯誤,當我收到消息:

01-07 08:44:38.190: E/AndroidRuntime(750): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

試圖找出什麼是最好的方式來處理,這將是,我需要緩存活動上下文在一個單身人士,然後從聽衆訪問單身人士?或者,還有更好的方法?

感謝

回答

1

這是不可能創建/顯示來自BroadcastReceiver一個Dialog。看到這個SO問題的答案android-broadcast-receiver-showing-a-dialog

還仔細考慮這樣做 - 正如問題答案中提到的那樣,當他們在做其他事情時,您是否真的想通過在用戶面前強制彈出窗口來惹惱用戶?

處理C2DM消息的首選方式通常是靜默和不可見處理,或者可能只是創建用戶以後可以採取行動的Notification

+0

感謝您的鏈接,這有助於清理事情。我要去通知 – Hoofamon 2012-01-07 19:58:04

+0

很高興提供幫助。我非常喜歡Android中的Notification系統,它非常靈活。 – Squonk 2012-01-07 20:08:54