2014-02-15 102 views
2

我的應用程序在後臺我用發送當滿足特定條件的廣播服務:如何顯示,從後臺活動啓動對話框

if(send) 
{ 
Intent intent = new Intent(Tags.MSG_BROADCAST_ID); 
intent.putExtra(Tags.MESSAGE, msg); 
Log.w(Tags.DEBUG,"Broadcast"); 
sendBroadcast(intent); 
} 
} 

我的廣播接收器獲取的廣播和它應該顯示一個警告對話框。我使用這個:

public void onReceive(Context context, Intent intent) 
    { 
    Bundle bundle = intent.getExtras(); 
    if (bundle != null && Globals.MainActivity != null) 
    { 
     msg = bundle.getString(Tags.MESSAGE); 
     Globals.MainActivity.ShowMessage(msg); 
    } 
    } 

當我的主要活動是在前臺,警報顯示正確。當它在背景中時,什麼都看不見。

Runnable runnable = new Runnable() 
    { 
     public void run() 
     { 
     KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); 
     final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("My_App"); 
     kl.disableKeyguard(); 
     PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App"); 
     wl.acquire(); 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       MainActivity.this); 

     // set title 
     alertDialogBuilder.setTitle("Your Title"); 

     // set dialog message 
     alertDialogBuilder 
       .setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        dialog.dismiss(); 
        } 
       } 
       ); 

     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
      alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
     alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
     // show it 
     alertDialog.show(); 
     wl.release(); 
     } 
    }; 
    runOnUiThread(runnable); 

我試圖用對話的主題活動,而不是對話的,但是它帶來的活動集中(我只需要在對話框中)。因爲我需要顯示在鎖定的屏幕,我加了一些標誌對話框,並在以下權限:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 

我測試了它在Android 2.3.6。

我該如何設置讓我的對話框可見?

+0

我不認爲你可以從後臺應用程序顯示對話框,因爲它不會訪問主線程,因此不會影響用戶界面。有一些小部件允許在iOS鎖定屏幕上進行背景通知。你應該使用谷歌,並看看那些。 – Rarw

回答

3

根據您的答案和一些搜索,我得到這樣的結果(可能不符合谷歌的UI指南):

創建一個代表警報

public class DialogMessageActivity extends Activity 

設置它的主題活動Theme.TranslucentAndroidManifest.xml中

android:theme="@android:style/Theme.Translucent" 

的onCreate

super.onCreate(savedInstanceState); 
//setContentView(R.layout.dialog_message); 

刪除其的setContentView功能添加AlertDialog並調用其顯示的onCreate

super.onCreate(savedInstanceState); 
//setContentView(R.layout.dialog_message); 
displayAlert(msg); 



private void displayAlert(String msg) 
    { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage(msg).setCancelable(
      false).setPositiveButton("OK", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
       finish(); 
       } 
      }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
    } 

當從任何其他活動調用它(前臺或後臺不管),請使用以下標誌:

Intent intent=new Intent(this,DialogMessageActivity.class); 
intent.putExtra(Tags.MESSAGE,msg); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

通過這些步驟,您可以創建一個與主應用程序看起來分離並單獨顯示的對話框。

1

問題是你不能自動顯示警告對話框沒有任何用戶界面,快速的解決辦法是,以顯示它時,你的應用程序恢復到前面的onResume()或通知,檢查that更多。

+0

我不明白。某些消息應用程序甚至在鎖定屏幕上顯示對話框(如V *** r)。他們怎麼能這樣做? – Nestor

+0

在我上次更新的鏈接,你可以找到很多的信息,bref你可以開始**活動與對話框主題**我認爲V使用類似的東西: http://developer.android.com/guide/topics/ui/ themes.html#ApplyATheme – Smile2Life

相關問題