3 回答

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
android-smspopup正是這樣做的。
服務(wù)接收到一條短信,并以開(kāi)頭Activity:
android:theme="@android:style/Theme.Dialog"
編輯:對(duì)話(huà)框活動(dòng)從此處使用此代碼開(kāi)始
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該類(lèi)顯然定義了對(duì)話(huà)框活動(dòng)。其聲明如下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>

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
服務(wù)中的材質(zhì)樣式對(duì)話(huà)框
從服務(wù)中,您可以輕松顯示“材質(zhì)設(shè)計(jì)”樣式的對(duì)話(huà)框,以操縱其窗口類(lèi)型,屬性和LayoutParams。
開(kāi)始之前:AppCompat庫(kù)
本指南假定您正在使用Android AppCompat libray。
開(kāi)始之前:權(quán)限
此方法需要SYSTEM_ALERT_WINDOW權(quán)限。通常,要顯示對(duì)話(huà)框的服務(wù)還具有一些在系統(tǒng)UI上繪制的視圖(使用WindowManager.addView()方法添加),因此您可能已經(jīng)在清單中聲明了此權(quán)限用法。如果沒(méi)有,請(qǐng)?zhí)砑右韵滦校?/p>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
在Android 6.0 Marshmallow中,用戶(hù)必須明確允許您的應(yīng)用“覆蓋其他應(yīng)用”。您可以以編程方式啟動(dòng)包含該開(kāi)關(guān)的系統(tǒng)設(shè)置活動(dòng):
@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;
}
}
創(chuàng)建您的自定義“材料設(shè)計(jì)對(duì)話(huà)框”主題
在內(nèi)部themes.xml創(chuàng)建此主題并使用您的應(yīng)用顏色對(duì)其進(jìn)行自定義:
<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>
啟動(dòng)對(duì)話(huà)框
服務(wù)內(nèi)幕:
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();
- 3 回答
- 0 關(guān)注
- 513 瀏覽
添加回答
舉報(bào)