簡體   English   中英

來自Android服務的警報對話框

[英]Alert dialog from Android service

如何從服務中顯示對話框?

android-smspopup正是如此。

服務接收短信並啟動一個Activity

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

編輯:對話活動開始在這里與此代碼

private void notifyMessageReceived(SmsMmsMessage message) {
    (...)
    context.startActivity(message.getPopupIntent());
    (...)
}

隨着getPopupIntent()聲明如下(代碼在這里 ):

public Intent getPopupIntent() {
    Intent popup = new Intent(context, SmsPopupActivity.class);
    popup.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    popup.putExtras(toBundle());
    return popup;
    }

SmsPopupActivity類顯然定義了對話框活動。 它在AndroidManifest.xml聲明為:

    <activity
        android:name=".ui.SmsPopupActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:launchMode="singleTask"
        android:screenOrientation="user"
        android:taskAffinity="net.everythingandroid.smspopup.popup"
        android:theme="@style/DialogTheme" >
    </activity>

來自服務的材質樣式對話框

在服務中,您可以輕松地顯示Material Design樣式的Dialog,以操縱其Window類型,屬性和LayoutParams。

在開始之前:AppCompat Library

本指南假設您使用的是Android AppCompat libray。

在我們開始之前:權限

此方法需要SYSTEM_ALERT_WINDOW權限。 通常,想要顯示對話框的服務也有一些通過系統UI繪制的視圖(使用WindowManager.addView()方法添加),因此您可能已經在清單中聲明了此權限用法。 如果沒有,請添加以下行:

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

Android 6.0 Marshmallow中 ,用戶必須明確允許您的應用“抽取其他應用”。 您可以以編程方式啟動包含該開關的系統設置Activity:

@Override
protected void onResume() {
    super.onResume();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
        openOverlaySettings();
    }
}

@TargetApi(Build.VERSION_CODES.M)
private void openOverlaySettings() {
    final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                     Uri.parse("package:" + getPackageName()));
    try {
        startActivityForResult(intent, RC_OVERLAY);
    } catch (ActivityNotFoundException e) {
        Log.e(TAG, e.getMessage());
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case RC_OVERLAY:
            final boolean overlayEnabled = Settings.canDrawOverlays(this);
            // Do something...
            break;
    }
}

創建自定義Material Design對話框主題

themes.xml創建此主題並使用您的應用顏色自定義:

<style name="AppTheme.MaterialDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorPrimary">@color/brand_primary</item>
    <item name="colorPrimaryDark">@color/brand_primary_dark</item>
    <item name="colorAccent">@color/brand_accent</item>

    <item name="android:windowBackground">@drawable/dialog_background_light</item>

    <item name="android:textColorPrimary">@color/primary_text_light</item>
    <item name="android:textColorSecondary">@color/secondary_text_light</item>
    <item name="android:textColorTertiary">@color/secondary_text_light</item>
</style>

啟動對話框

在您的服務內:

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.AppTheme_MaterialDialogTheme);

dialogBuilder.setTitle(R.string.dialog_title);
dialogBuilder.setMessage(R.string.dialog_message);
dialogBuilder.setNegativeButton(R.string.btn_back,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }
);

final AlertDialog dialog = dialogBuilder.create();
final Window dialogWindow = dialog.getWindow();
final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();

// Set fixed width (280dp) and WRAP_CONTENT height
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialogWindowAttributes);
lp.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 280, getResources().getDisplayMetrics());
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialogWindow.setAttributes(lp);

// Set to TYPE_SYSTEM_ALERT so that the Service can display it
dialogWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialogWindowAttributes.windowAnimations = R.style.DialogAnimation;
dialog.show();

不使用活動的另一種方法:

AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

請注意,您必須使用此權限:

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

文檔建議您應該使用通知。 重新評估您可能需要使用對話框的原因。 你想要實現什么目標?

第一次你需要在服務中投射你的活動,所以在你的活動中添加

public static Activity mactivity;

並在On create中添加此項

mactivity = YourActivity.this;

當我們轉到你的服務部門時,聲明:

YourActivity mact;
YourActivity act;

在創建服務這是我們的演員

mact = (YourActivity) act.mactivity;

並且警報對話框將如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(mact);
        builder.setMessage(getResources().getString(R.string.string));
        builder.setIcon(R.drawable.chat);
        builder.setTitle(R.string.app_name);
        builder.setPositiveButton(getResources().getString(R.string.Ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
               your message
            }
        });

請記住, mact是在Alert Builder中使用的Cast類...對我來說很好,希望它有所幫助。

當我需要顯示對話框時,我在服務內部調用下面的內容。

public void ShowYesNoDialog() {

    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes Button Clicked
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Did the dialog display?")
    .setPositiveButton("Yes", dialogClickListener)
    .setNegativeButton("No", dialogClickListener);
    AlertDialog alertDialog = builder.create();
    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    alertDialog.show();
}

確保將以下權限添加到清單中。 android.permission.SYSTEM_ALERT_WINDOW

我還認為對於SDK 23及更高版本,用戶應該從應用程序管理器為啟動此服務的應用程序明確設置“Draw over other Apps”權限。

以下是如何使用半透明Activity從服務中顯示AlertDialog以及如何避免某些問題的更詳細說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM