我的应用程序有一个接收器,在收到SMS时会调用它。我想通过一个简单的1按钮对话框通知用户..
这是我的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Hello dude").setCancelable(false).setPositiveButton("Got you", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//mp.stop();
}
}).show();
但是,这是一个例外:
android.view.WindowManager $ BadTokenException
请帮忙......
答案 0 :(得分:5)
首先,您无法显示Service
或BroadcastReceiver
。
其次,请不要中断用户。让用户知道在后台发生的类似事情的正确方法是显示Notification
。
答案 1 :(得分:2)
以下是我所做的事情:从我的服务实例开始,我开始这样的活动:
final Intent dialog = new Intent(this, BackGroundDialogs.class);
dialog.setType("text/plain");
dialog.putExtra(android.content.Intent.EXTRA_TEXT, reason);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(dialog);
对于BackGround Dialogs类,它看起来像这样:
/**
* @ To create the illusion of a alert window displayed on its own when app is
* in the background. Really, this is a Activity but its only displaying an
* alert window and the Activity borders have been removed.
*/
public class BackGroundDialogs extends Activity {
public BroadcastReceiver receiver;
public AlertDialog mAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // custom theme with
// no borders
IntentFilter filter = new IntentFilter();
filter.addAction(Consts.DISMISS_DIALOG);// we can dismiss it via an
// intent if we choose
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, final Intent intent) {
// do something based on the intent's action
if (context == null) return;
if (intent.getAction().equals(Consts.DISMISS_DIALOG)) {
finish();
}
}
};
registerReceiver(receiver, filter);
}
/**
* @brief Shows an alert message using a Dialog window.
* @param reason
* :the message you wish to display in the alert
*/
public void showAlert(final String reason) {
mAlert = new AlertDialog.Builder(this).create();
mAlert.setCancelable(false);
TextView Msg_tv = new TextView(this);
Msg_tv.setTypeface(null, Typeface.BOLD);
Msg_tv.setTextSize(16.0f);
Msg_tv.setText(reason);
Msg_tv.setGravity(Gravity.CENTER_HORIZONTAL);
mAlert.setView(Msg_tv);
mAlert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
mAlert.show();
}
@Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
String reason = extras.getString(Intent.EXTRA_TEXT);
if (reason.equalsIgnoreCase("DISMISS")) finish();
else showAlert(reason);// invoke the new dialog to show
}
@Override
protected void onPause() {
super.onPause();
if (mAlert != null) if (mAlert.isShowing()) mAlert.dismiss();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
您不必使用BroadcastReceiver调用,我只是认为如果需要,可以方便地从其他地方控制警报。
答案 2 :(得分:0)
...拉希姆 您可以按照从activiy
开始活动的方式从服务开始活动Intent newActivity = new Intent(context,servicename.class);
context.startService(newActivity);
上下文应该是应用程序上下文
答案 3 :(得分:0)
您可以使用TYPE_SYSTEM_ALERT
:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Hello dude")
.setCancelable(false)
.setPositiveButton("Got you", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//mp.stop();
}
}).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
请注意,您必须使用以下权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />