来自Android服务的警报对话框

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



SMILET
浏览 497回答 3
3回答

婷婷同学_

android-smspopup正是这样做的。服务接收到一条短信,并以开头Activity:android:theme="@android:style/Theme.Dialog"编辑:对话框活动从此处使用此代码开始private void notifyMessageReceived(SmsMmsMessage message) {&nbsp; &nbsp; (...)&nbsp; &nbsp; context.startActivity(message.getPopupIntent());&nbsp; &nbsp; (...)}随着getPopupIntent()声明如下(代码在这里):public Intent getPopupIntent() {&nbsp; &nbsp; Intent popup = new Intent(context, SmsPopupActivity.class);&nbsp; &nbsp; popup.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);&nbsp; &nbsp; popup.putExtras(toBundle());&nbsp; &nbsp; return popup;&nbsp; &nbsp; }SmsPopupActivity该类显然定义了对话框活动。其声明如下AndroidManifest.xml:&nbsp; &nbsp; <activity&nbsp; &nbsp; &nbsp; &nbsp; android:name=".ui.SmsPopupActivity"&nbsp; &nbsp; &nbsp; &nbsp; android:configChanges="keyboardHidden|orientation|screenSize"&nbsp; &nbsp; &nbsp; &nbsp; android:launchMode="singleTask"&nbsp; &nbsp; &nbsp; &nbsp; android:screenOrientation="user"&nbsp; &nbsp; &nbsp; &nbsp; android:taskAffinity="net.everythingandroid.smspopup.popup"&nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/DialogTheme" >&nbsp; &nbsp; </activity>

暮色呼如

服务中的材质样式对话框从服务中,您可以轻松显示“材质设计”样式的对话框,以操纵其窗口类型,属性和LayoutParams。开始之前:AppCompat库本指南假定您正在使用Android AppCompat libray。开始之前:权限此方法需要SYSTEM_ALERT_WINDOW权限。通常,要显示对话框的服务还具有一些在系统UI上绘制的视图(使用WindowManager.addView()方法添加),因此您可能已经在清单中声明了此权限用法。如果没有,请添加以下行:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />在Android 6.0 Marshmallow中,用户必须明确允许您的应用“覆盖其他应用”。您可以以编程方式启动包含该开关的系统设置活动:@Overrideprotected void onResume() {&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {&nbsp; &nbsp; &nbsp; &nbsp; openOverlaySettings();&nbsp; &nbsp; }}@TargetApi(Build.VERSION_CODES.M)private void openOverlaySettings() {&nbsp; &nbsp; final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Uri.parse("package:" + getPackageName()));&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent, RC_OVERLAY);&nbsp; &nbsp; } catch (ActivityNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; Log.e(TAG, e.getMessage());&nbsp; &nbsp; }}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; switch (requestCode) {&nbsp; &nbsp; &nbsp; &nbsp; case RC_OVERLAY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final boolean overlayEnabled = Settings.canDrawOverlays(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do something...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}创建您的自定义“材料设计对话框”主题在内部themes.xml创建此主题并使用您的应用颜色对其进行自定义:<style name="AppTheme.MaterialDialogTheme" parent="Theme.AppCompat.Light.Dialog">&nbsp; &nbsp; <item name="colorPrimary">@color/brand_primary</item>&nbsp; &nbsp; <item name="colorPrimaryDark">@color/brand_primary_dark</item>&nbsp; &nbsp; <item name="colorAccent">@color/brand_accent</item>&nbsp; &nbsp; <item name="android:windowBackground">@drawable/dialog_background_light</item>&nbsp; &nbsp; <item name="android:textColorPrimary">@color/primary_text_light</item>&nbsp; &nbsp; <item name="android:textColorSecondary">@color/secondary_text_light</item>&nbsp; &nbsp; <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,&nbsp; &nbsp; &nbsp; &nbsp; new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });final AlertDialog dialog = dialogBuilder.create();final Window dialogWindow = dialog.getWindow();final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();// Set fixed width (280dp) and WRAP_CONTENT heightfinal 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 itdialogWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);dialogWindowAttributes.windowAnimations = R.style.DialogAnimation;dialog.show();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android