[英]How to create a android alertdialog in phonestatelistener?
The code looks like this: 代码如下:
1) Declare the new activity in manifest file. 1)在清单文件中声明新活动。 (AndroidManifest.xml): (AndroidManifest.xml):
<activity
android:name=".ConfirmDialog"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"
android:launchMode="singleTask"
android:screenOrientation="vertical">
</activity>
2) Create new class extends activity. 2)创建新的类扩展活动。 (public class ConfirmDialog extends Activity) (公共类ConfirmDialog扩展Activity)
private static final int DIALOG_YES_NO_MESSAGE = 1;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_YES_NO_MESSAGE:
return new AlertDialog.Builder(ConfirmDialog.this)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.app_name)
.setMessage(R.string.ask_confirm)
.setPositiveButton(R.string.ask_confirm_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
finish();
}
})
.setNegativeButton(R.string.ask_confirm_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
finish();
}
})
.create();
}
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog(DIALOG_YES_NO_MESSAGE);
}
3) Use the new created activity in phonestatelistener. 3)在phonestatelistener中使用新创建的活动。 (public class Listener extends PhoneStateListener) (公共类Listener扩展了PhoneStateListener)
public void onCallStateChanged(int state, String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_OFFHOOK:
confirmCall();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
private void confirmCall(){
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.ConfirmDialog"));
mContext.startActivity(intent);
}
Not all Context
objects are equal, and reusing them is dangerous because it can result in the error that you're seeing. 并非所有Context
对象都是相等的,并且重用它们很危险,因为它可能会导致您看到的错误。 Per your comment, your revised code is storing a Context
from SherlockFragmentActivity in your Config
class, and then using it later when that Activity
(and the window that it was using) may not exist anymore. 根据您的评论,修改后的代码将SherlockFragmentActivity中的Context
存储在Config
类中,然后在该Activity
(及其使用的窗口)不再存在时再使用它。 The Context
isn't null and can probably be used to do a Toast
, but doing windowing operations is very risky. Context
不为null,可能可以用来做Toast
,但是进行窗口操作非常冒险。
One solution is to create a dialog-themed activity in your app that will only show the dialog. 一种解决方案是在您的应用中创建一个以对话框为主题的活动,该活动仅显示该对话框。 In confirmCall()
, launch your activity with startActivity()
, and then let the activity create the dialog. 在confirmCall()
,使用startActivity()
启动您的活动,然后让该活动创建对话框。 To the user, a dialog will appear on the screen without an app running, but in fact your app is running and can respond to the user's Yes/No choice on the dialog. 对于用户,没有应用程序运行时,会在屏幕上显示一个对话框,但是实际上您的应用程序正在运行,并且可以在对话框上响应用户的是/否选择。 Be sure to use 请务必使用
new AlertDialog.Builder(this)
so that the dialog's Context
is an Activity
. 这样对话框的Context
是一个Activity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.