来自Android服务的警报对话框

d-m*_*man 26 service android

如何从服务中显示对话框?

Mac*_*rse 22

android-smspopup正是如此.

服务接收短信并开始Activity使用:

android:theme="@android:style/Theme.Dialog"
Run Code Online (Sandbox Code Playgroud)

编辑:该对话框的活动开始在这里与此代码

private void notifyMessageReceived(SmsMmsMessage message) {
    (...)
    context.startActivity(message.getPopupIntent());
    (...)
}
Run Code Online (Sandbox Code Playgroud)

随着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;
    }
Run Code Online (Sandbox Code Playgroud)

SmsPopupActivityclass明确定义了对话活动.其声明如下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>
Run Code Online (Sandbox Code Playgroud)

  • 如果可能,请参考确切的代码段.日Thnx (2认同)

Eli*_*uta 22

不使用活动的另一种方法:

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

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)

请注意,您必须使用此权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Run Code Online (Sandbox Code Playgroud)

  • 在为调试目的启动我的服务后,只需要一种快速的方法来选择某些东西,这正是我正在寻找的。 (3认同)
  • TYPE_SYSTEM_ALERT 已弃用,请使用 TYPE_APPLICATION_OVERLAY https://developer.android.com/reference/android/view/WindowManager.LayoutParams#TYPE_SYSTEM_ALERT (2认同)

ara*_*aks 20

来自服务的材质样式对话框

在服务中,您可以轻松地显示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" />
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建自定义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>
Run Code Online (Sandbox Code Playgroud)

启动对话框

在您的服务内:

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();
Run Code Online (Sandbox Code Playgroud)


小智 5

文档建议您应该使用通知.重新评估您可能需要使用对话框的原因.你想要实现什么目标?

  • 也许是一个闹钟?有时候有弹出的理由! (26认同)