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)
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)
ara*_*aks 20
在服务中,您可以轻松地显示Material Design样式的Dialog,以操纵其Window类型,属性和LayoutParams.
本指南假设您使用的是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)
在里面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)
| 归档时间: |
|
| 查看次数: |
47535 次 |
| 最近记录: |