2017-02-04 27 views
1

我有一個IntentService,我試圖在一些過程完成後顯示帶有消息的對話框。通過IntentService顯示對話框?

我可以通過做

handler.post(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(ServiceName.this, message, Toast.LENGTH_LONG); 
     } 
    }); 

顯示敬酒,但我想顯示一個對話框來代替,因爲它可以顯示更多的文字,並尋找什麼,我試圖做清潔。但是,當我嘗試:

handler.post(new Runnable() { 
     @Override 
     public void run() { 
      new AlertDialog.Builder(ServiceName.this) 
        .setTitle("Title") 
        .setMessage(message) 
        .create().show(); 
     } 
    }); 

它拋出一個錯誤:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我不想去與我的清單擺弄周圍,因爲這可能會影響到應用程序的一切,但沒有任何繞過這個方法?我可以以某種方式將AppCompatActivity傳遞給IntentService嗎?我可以以某種方式將IntentService與AppCompat關聯?我有什麼選擇?

編輯:嘗試使用對話框活動辦法:

Intent intent = new Intent(ServiceName.this, ActivityWithDialogTheme.class); 
intent.putExtra(ActivityWithDialogTheme.MESSAGE, message); 
startActivity(intent); 

回答

1

Can I somehow pass an AppCompatActivity into the IntentService?

Can I somehow associate the IntentService with AppCompat?

What are my options?

建立對話爲主題的活動,並從您的服務開始該活動。

或者,讓服務在事件總線上發佈消息,告知程序已完成。如果您碰巧在前臺有活動,它可以選取事件總線消息並顯示對話框。如果您在前臺沒有進行任何活動,您的服務可以找到並執行其他操作(例如,顯示Notification),以便不中斷用戶。

+0

你在會議公共汽車上張貼什麼意思?我如何獲得一個活動來撿起它?至於通知我並不熟悉,但似乎是學習的好東西 – KaliMa

+0

@KaliMa:「在會展巴士上發佈什麼意思?」 - 我的意思是使用事件總線,[如LocalBroadcastManager](https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/LocalBroadcastManager)或[greenrobot的EventBus](https:// github的.com/commonsguy /順式總括/樹/主/ EventBus/GreenRobot3)。 「我如何得到一個活動來撿起它?」 - 這是使用事件總線的一部分。本評論前面的鏈接是示例項目,演示如何使用這兩種總線,UI或「通知」是服務工作的結果(在這種情況下,由「AlarmManager」觸發)。 – CommonsWare

+0

這是否意味着我沒有選擇,如果我不知道如何在這種情況下使用EventBus? – KaliMa