所以我试图在一个活动中处理传入的短信 BroadCastReciever如此:
protected void onCreate(Bundle savedInstanceState) {
BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Log.i("SMSRECEIVER", "SMS RECIEVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Log.i("SMSRECEIVER", "Something happened?");
}
};
};
registerReceiver(smsReceiver, new IntentFilter());
}
然而,它没有做任何事情(根据公司短信)。
另请注意,我确实将android.permission.RECEIVE_SMS作为一个 我的清单中的许可。
编辑:仍然没有做任何事情。
答案 0 :(得分:1)
看一下这段代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
};
registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
这里我使用SmsMessage类访问消息和号码。 在清单中,我提供以下权限。
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
答案 1 :(得分:0)
基于ρяσѕρєяK注释,你应该像这样使用Log而不是system.out.println:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Log.i("My Tag","SMS RECIEVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Log.i("My Tag","Something happened?");
}
答案 2 :(得分:0)
这里尝试使用这个SmsListener接收器:
public class SmsListenerManager extends BroadcastReceiver {
private SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
//---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
System.out.println(msgBody+" <<< Message Body");
}
} catch (Exception e) {
Log.d("Exception caught",e.getMessage());
}
}
}
}
}
将此添加到您的清单:
许可:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
在申请标签下:
<receiver android:name=".SmsListenerManager">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
我希望它有所帮助。感谢