申请权限
<!--短信广播监听器-->
<receiver android:name=".Broadcast">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class Broadcast extends BroadcastReceiver {
private static final String ACTION="android.provider.Telephony.SMS_RECEIVED";
private Message message=new Message();
private Bundle bundle=new Bundle();
@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "来短信啦",Toast.LENGTH_SHORT).show();
if(ACTION.equals(intent.getAction())){
SmsMessage[] smsMessage=getMessageFromIntent(intent);
StringBuilder str=new StringBuilder();
if(smsMessage!=null&&smsMessage.length>0){
for(SmsMessage smg:smsMessage){
str.append(smg.getDisplayOriginatingAddress()+":\n ");
str.append(smg.getDisplayMessageBody()+"\n");
}
}
Toast.makeText(context, str.toString(),Toast.LENGTH_SHORT).show();
}
}
public static SmsMessage[] getMessageFromIntent(Intent intent){
SmsMessage[] smsMessage=null;
Bundle bundle=intent.getExtras();
Object[] puds=(Object[])bundle.get("puds");
smsMessage=new SmsMessage[puds.length];
for(int i=0;i<puds.length;i++){
byte[] data=((byte[])puds[i]);
smsMessage[i]=SmsMessage.createFromPdu(data);
}
return smsMessage;
}
}
请问哪里错了就是监听不了短信,有人发短信来没app没反应
不好意思我小白一个没有C币,求助