如何在收到电话状态时打开Dialog
。因为无论何时收到State
,都会导致以下错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我该如何解决这个问题?
电话状态是空闲然后我打电话给Dialog。任何帮助将不胜感激。这是我的代码。
final Dialog dialog = new Dialog(mcontext);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Android Custom Dialog Box");
final EditText et_remark = (EditText) dialog
.findViewById(R.id.et_remark);
Button dialogButton = (Button) dialog
.findViewById(R.id.btn_submit);
dialogButton
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
String remark = et_remark.getText()
.toString();
if (call_duration.equals("0")) {
Toast.makeText(mcontext,
" miss",
Toast.LENGTH_LONG)
.show();
} else {
if (cType.equals("OUTGOING")) {
Toast.makeText(
mcontext,
" out ",
Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(mcontext,
" inc",
Toast.LENGTH_LONG)
.show();
}
}
}
});
dialog.show();
答案 0 :(得分:0)
广播接收是应用服务(意向服务)。 intent service对第一个有一些限制,最重要的是'它无法直接与您的用户界面进行互动。
然后你有一个替代方案,你可以尝试使用句柄,如果你的应用程序打开,然后句柄捕获消息并从句柄抛出对话框。 告诉我,如果我帮助你并做好编程!
答案 1 :(得分:0)
试试这个
广播接收器
public class MyCallReceiver extends BroadcastReceiver {
private String incomingNumber;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// get the phone number
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent i = new Intent(context, Disp_Alert_dialog.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//i.putExtra("Number", incomingNumber);
//i.putExtra("type", "incoming");
context.startActivity(i);
// Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
// This code will execute when the phone has an incoming call
} else {
// This code will execute when the call is disconnected
// Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}
}
Alertclass.java
public class Disp_Alert_dialog extends Activity{
private String nums;
private String outnum;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent iin= getIntent();
nums=iin.getStringExtra("Number");
// Toast.makeText(Disp_Alert_dialog.this, "Got it"+nums, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("")
.setMessage("")
.setCancelable(false)
.setPositiveButton("yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Intent intent=new Intent(Disp_Alert_dialog.this,MainActivity.class);
intent.putExtra("Nums", nums);
startActivity(intent);
Disp_Alert_dialog.this.finish();
/*HomeFragment fragobj = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putString("Nums", nums);
// set Fragmentclass Arguments
fragobj.setArguments(bundle);*/
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}