短信弹出:AlertDialog不显示,因为我收到短信
问题描述:
我看到很多应用程序做短信弹出窗口。为什么我无法使我的应用程序工作?如果有短信息,我希望它在屏幕上弹出。短信弹出:AlertDialog不显示,因为我收到短信
这里是我的代码:
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();
}
}
};
}
答
我觉得这里的问题是与上下文对象。来自
的onReceive(上下文语境,意图意图)
你应该通过上下文onRecive收到喜欢 ..
私人无效displayAlert(上下文语境)
然后, 变化,
AlertDialog.Builder助洗剂=新AlertDialog.Builder(本);
TO
AlertDialog.Builder助洗剂=新AlertDialog.Builder(上下文);
现在它应该work.hope这有助于。
干杯。
+0
我做了改变AlertDialog仍然不弹出烤面包 – 2011-02-06 22:21:30
当收到新的SMS消息时,有一些移动部件可以显示弹出窗口。你可以发布你的Android Manifest以及它是如何工作的一个重要部分。 – 2011-08-25 16:38:52