问题
How to open Dialog
when phone state is receive. Because whenever a State
is received it results in the following error:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
How can I solve this problem?
Phone State is Idle then i call Dialog. Any help will be appreciated. Here is my code.
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();
回答1:
The broadcast receive is app service(intent service). The intent service have a few limitations the first one and most important is 'It can't interact directly with your user interface'.
Then you have a alternative you can try to work with handle if your app it's open and then the handle captures message and throws dialog from handle. Tell me if I helped you and good programming!
回答2:
Try like this
Broadcastreceiver
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();
}
}
来源:https://stackoverflow.com/questions/30908995/how-to-open-dialog-box-when-broadcast-receive-in-android