2015-11-04 92 views
1

如何在IntentService中顯示AlertDialog?錯誤如下所示: 無法添加窗口 - 標記null不適用於應用程序。 所以這是上下文的問題,但我不知道如何解決它。任何解決方案 下面是我的代碼:在IntentService中顯示AlertDialog

public class GcmMessageHandler extends IntentService { 

private GoogleCloudMessaging gcm; 
private static AlertDialog alert; 

String mes; 
private Handler handler; 
public GcmMessageHandler() { 
    super("GcmMessageHandler"); 
} 

@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 
    handler = new Handler(); 

} 
@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 

    gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 

    mes = extras.getString("message"); 
    MApp.wakeupSync(); 

    showToast(); 
    Log.i("GCM", "Received : (" + messageType + ") " + extras.getString("message")); 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

@Override 
public void onDestroy() { 
    try { 
     gcm.unregister(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void showToast(){ 

    handler.post(new Runnable() { 
     public void run() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
      builder.setMessage("TEST") 
        .setCancelable(false) 
        .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      alert = builder.create(); 
      alert.show(); 
     } 
    }); 

} 
} 
+0

我認爲你唯一可以做的事情,就是啓動這表明你對話的活動。您可以使活動的背景透明。 – Christopher

+0

將結果返回到您已經開始的地方。 –

+0

展會活動爲對話框,<活動安卓主題=「@安卓風格/ Theme.Dialog」 /> – rajahsekar

回答

0

您只能使用活動上下文顯示AlertDialog。

在這裏,您使用getApplicationContext()創建的建設者,但 既不應用程序上下文,也服務方面將起作用。 您必須將廣播/意圖發送給某個Activity,並且作爲對此消息的響應,使用Activity上下文在此Activity中顯示AlertDialog。

閱讀此瞭解更多詳情: https://possiblemobile.com/2013/06/context/

+0

我的辦法是StartNotification ,發送價值使用braodcast意圖,並在意圖顯示警報 – user5523912

+0

我不明白你的意思是「在意圖中顯示警報」 這裏的重點不是使用getApplicationContext()它不能工作。 改爲使用任何活動上下文。 –

+0

在通知中選擇的活動中的AlertDialog – user5523912

0

Activity

/** 
    * Response handler 
    */ 
    private Handler handlerResponse = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
     //Show your alert here 
     } 
    }; 

啓動服務以及Messenger

Intent intent = new Intent(getActivity(), CommunicationService.class); 
intent.setAction(Constants.ACTION_ONE); 
intent.putExtra(Constants.EXTRA_MESSENGER, new Messenger(handlerResponse)); 
startService(intent); 

在服務完成後返回的消息定義處理程序功能

protected void onHandleIntent(Intent intent) { 
     if (intent != null) { 
      sendResult(intent, result); 
     } 
    } 

private void sendResult(Intent intent, String result) { 
     Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      Messenger messenger = (Messenger) extras.get(Constants.EXTRA_MESSENGER); 
      Message msg = Message.obtain(); 
      msg.obj = result; 
      try { 
       messenger.send(msg); 
      } catch (Exception e1) { 
       Log.e(getClass().getName(), "Exception sending message", e1); 
      } 
     } 
    } 

常量

public static final String EXTRA_MESSENGER = "EXTRA_MESSENGER"; 
public static final String ACTION_ONE = "INIT_SOCKET"; 
+0

我的方法是使用braodcast將意向發送到StartNotification,並在意圖中顯示警報 – user5523912