Android 短信弹出窗口:收到短信时不会显示AlertDialog
我看到很多应用程序都有短信弹出窗口。为什么我的应用程序不能正常工作?如果有短信进来,我希望它能在屏幕上弹出 这是我的密码:Android 短信弹出窗口:收到短信时不会显示AlertDialog,android,sms,android-alertdialog,Android,Sms,Android Alertdialog,我看到很多应用程序都有短信弹出窗口。为什么我的应用程序不能正常工作?如果有短信进来,我希望它能在屏幕上弹出 这是我的密码: public class NotifySMSReceived extends Activity { private static final String LOG_TAG = "SMSReceiver"; public static final int NOTIFICATION_ID_RECEIVED = 0x1221; static fin
public class NotifySMSReceived extends Activity
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(mReceivedSMSReceiver, filter);
}
private void displayAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION.equals(action))
{
//your SMS processing code
displayAlert();
}
}
};
}
我认为这里的问题在于上下文对象。从 onReceiveContext上下文、意图 您应该将onRecive中接收的上下文传递给like 私有void displayAlertContext上下文 然后呢,, 变, AlertDialog.Builder=新建AlertDialog.Builder此 到 AlertDialog.Builder=新建AlertDialog.Buildercontext 现在它应该可以工作了。希望这有帮助
干杯。我做了更改,AlertDialog仍然不会弹出Toast doeshave您是否在AlertDialog中传递了接收者的上下文?如果是这样的话,它应该会起作用。post错误日志(如果有)。显示Toast是因为它显示为系统覆盖。AlertDialog仅在SMS到达时将应用程序置于前台时显示。我选中并看到AlertDialog,但只有当我导航到我的应用程序时。如果你想在屏幕上的任何内容上强制显示一些内容,这不是用户友好的,但是可行的。试试Zelimir,但我正在尝试制作一个类似SMS的应用程序。他是如何工作的?当收到新的短信时,会有一个弹出窗口显示移动部件。你能发布你的Android清单吗?这是它工作原理中的一个重要部分。