我正在尝试使用SMS Broadcast Receiver自动将消息读入EditText。当我正在注册BroadcastReceiver错误时出现“Expression Expected”
IntentFilter smsFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(SMSReceiver, smsFilter); //Expression Expected error occurred here
已解决:
我刚刚添加了新的this.registerReceiver(new SMSReceiver(), smsFilter);
而不是this.registerReceiver(SMSReceiver, smsFilter);
我希望这对你有所帮助。
答案 0 :(得分:2)
第一个参数应该是new SMSReceiver()
,而不是@ρяσѕρєяK的评论中提到的类名SMSReceiver
。
我建议您在onCreate()
中对其进行实例化,然后在registerReceiver()
和unregisterReceiver()
// in onCreate or globally
SMSReceiver smsReceiver= new SMSReceiver();
...
// and then in onResume
IntentFilter smsFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, smsFilter);
....
// and in pause
unregisterReceiver(smsReceiver);
答案 1 :(得分:1)
您是否在清单中添加以下代码?
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
如果没有,请添加此内容。
希望 SMSReceiver 是 BroadcastReceiver&#39> 实例。如果没有,则创建BroadcastReceiver的实例