短信弹出:AlertDialog不显示,我得到一个短信 [英] SMS Popup: AlertDialog does not show as I get a SMS message
问题描述
我看到了很多的做短信弹出应用程序。为什么我不能让我的应用程序工作吗?如果一个短信进来,我想它在屏幕上弹出。
下面是我的code:
公共类NotifySMSReceived扩展活动
{ 私有静态最后弦乐LOG_TAG =SMSReceiver; 公共静态最终诠释NOTIFICATION_ID_RECEIVED = 0x1221; 静态最后弦乐ACTION =android.provider.Telephony.SMS_RECEIVED;
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); IntentFilter的过滤器=新的IntentFilter(ACTION); this.registerReceiver(mReceivedSMSReceiver,过滤器); }
私人无效displayAlert() { AlertDialog.Builder建设者=新AlertDialog.Builder(本); builder.setMessage(你确定要退出?)。setCancelable( 假).setPositiveButton(是,
新DialogInterface.OnClickListener(){ 公共无效的onClick(DialogInterface对话,诠释的id){
dialog.cancel();
}
})。setNegativeButton(否,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
dialog.cancel();
}
});
AlertDialog警报= builder.create();
alert.show();
} 私人最终的BroadcastReceiver mReceivedSMSReceiver =新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction(); 如果(ACTION.equals(动作))
{
//你的短信处理code
displayAlert();
}
}
};
}
我觉得这里的问题是与上下文对象。从
的onReceive(上下文的背景,意图意图)
块引用>您应该通过的背景在onRecive收到喜欢..
私人无效displayAlert(背景下的背景)
块引用>和然后,
变化,
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
块引用>到
AlertDialog.Builder建设者=新AlertDialog.Builder(背景);
块引用>现在应该work.hope这会有所帮助。
干杯。
I see a lot of applications that do SMS popups. Why can't I get my app working? If a SMS message comes in, I would like it to popup on the screen.
Here's my code:
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(); } } }; }
解决方案I think problem here is with context object. From,
onReceive(Context context, Intent intent)
You should pass context received in onRecive to like..
private void displayAlert(Context context)
and then, change ,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TO
AlertDialog.Builder builder = new AlertDialog.Builder(context);
now it should work.hope this helps.
Cheers.
这篇关于短信弹出:AlertDialog不显示,我得到一个短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!