为了账号安全,请及时绑定邮箱和手机立即绑定

来自Android服务的警报对话框

来自Android服务的警报对话框

SMILET 2019-11-19 09:57:03
如何显示服务中的对话框?
查看完整描述

3 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

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>


查看完整回答
反对 回复 2019-11-19
?
暮色呼如

TA贡献1853条经验 获得超9个赞

服务中的材质样式对话框

从服务中,您可以轻松显示“材质设计”样式的对话框,以操纵其窗口类型,属性和LayoutParams。


开始之前:AppCompat库

本指南假定您正在使用Android AppCompat libray。


开始之前:权限

此方法需要SYSTEM_ALERT_WINDOW权限。通常,要显示对话框的服务还具有一些在系统UI上绘制的视图(使用WindowManager.addView()方法添加),因此您可能已经在清单中声明了此权限用法。如果没有,请添加以下行:


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

在Android 6.0 Marshmallow中,用户必须明确允许您的应用“覆盖其他应用”。您可以以编程方式启动包含该开关的系统设置活动:


@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;

    }

}

创建您的自定义“材料设计对话框”主题

在内部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();


查看完整回答
反对 回复 2019-11-19
  • 3 回答
  • 0 关注
  • 496 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信