如何在 android 中接收广播时打开对话框?
How to open Dialog Box when broadcast receive in android?
接收phone状态时如何打开Dialog
。因为每当收到 State
时,都会导致以下错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我该如何解决这个问题?
Phone State is Idle 然后我调用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();
广播接收是app service(intent service)。 intent service 有一些限制,第一个也是最重要的是“它不能直接与您的用户界面交互”。
然后你有一个替代方法,如果你的应用程序打开,你可以尝试使用 handle,然后 handle 捕获消息并从 handle 中抛出对话框。
告诉我我是否帮助了你和好的编程!
这样试试
广播接收器
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();
}
}
接收phone状态时如何打开Dialog
。因为每当收到 State
时,都会导致以下错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
我该如何解决这个问题?
Phone State is Idle 然后我调用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();
广播接收是app service(intent service)。 intent service 有一些限制,第一个也是最重要的是“它不能直接与您的用户界面交互”。
然后你有一个替代方法,如果你的应用程序打开,你可以尝试使用 handle,然后 handle 捕获消息并从 handle 中抛出对话框。 告诉我我是否帮助了你和好的编程!
这样试试
广播接收器
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();
}
}