我正在使用 AlarmManager
将 PendingIntent
传递到我的 BroadcastReceiver
之后,比方说,5 分钟。现在,我想向用户显示 DialogFragment
,在闹钟响起时用户可能正在使用的任何应用之上。例如,如果闹钟响起时用户正在使用 Chrome,我的 DialogFragment
应该在用户的 Chrome 窗口上方弹出。
我最终得到的是 DialogFragment
以我的应用程序的空白 Activity 作为背景显示(如下图所示)
http://i.stack.imgur.com/Vz9IZ.png
这是我在 BroadcastReceiver
中使用的代码,首先启动一个 FragmentActivity
:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent hardReminderIntent = new Intent(context, AlarmHardActivity.class);
hardReminderIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(hardReminderIntent);
}
}
接下来,从这个 Activity 中,我弹出 DialogFragment
:
public class AlarmHardActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
AlarmHardDialog editNameDialog = new AlarmHardDialog();
editNameDialog.show(fm, "fragment_dialog");
//setContentView(R.layout.activity_alarm_hard);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_alarm_hard, menu);
return true;
}
}
我的问题:
我找不到直接从
BroadcastReceiver
中的onReceive
调用getSupportFragmentManager
的方法,因此假设唯一获得对话的方法是首先调用创建对话的“虚拟” Activity 。有更好的方法吗?无论我的方法是否正确,我预计由于在 AlarmHardActivity 中没有调用
setContentView(..)
,所以不会为其呈现 UI。我的理解错了吗?我还尝试调用setContentView(..)
,然后将布局标记为具有Theme.NoDisplay
和android:alpha="0"
,但是无济于事。
任何帮助将不胜感激。谢谢!
最佳答案
不是在 Activity 上显示对话框,更好的替代方法是通过使用对话框主题并将所有边距设置为理想的 dp,使 Activity 看起来像对话框,这样 Activity 就会尖叫到对话框大小。
举几个例子看看这些帖子: Android Activity as a dialog
关于android - 在没有调用 Activity 的情况下显示 DialogFragment 作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14685582/