传入消息广播接收器不工作



大家好。 我向一台设备发送消息,并在另一台设备的应用程序中接收此短信。我使用Broascast接收器收听短信正文或号码。我已经在清单中获得了所有许可,但我的朗诵者没有打电话。我在 2 天内使用了很多与清单相关的东西,例如 android:priority,android:enabled="true",android:exporte,但 stiil 接收器不起作用。

/* final SmsManager sms = SmsManager.getDefault();*/
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "in receiver", Toast.LENGTH_SHORT).show();
    // TODO Auto-generated method stub
    Log.e("out", "out");
    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;
        Toast.makeText(context, "broad cast reciver", Toast.LENGTH_SHORT).show();
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                Log.e("in", "in");
                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();
                }
            }catch(Exception e){

Log.d("Exception caught",e.getMessage(((; } } } }

清单。。。。 <</p>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />

<application android:icon="@drawable/ic_launcher">
    <activity android:name=".MainActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".SmsListener" android:enabled="true"      android:exported="true" android:permission="android.permission.BROADCAST_SMS"> 
        <intent-filter android:priority="1000"> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
        </intent-filter> 
    </receiver>
 </application>

让您的 SmsListener 扩展 BroadcastReceiver:

public class SmsListener extends BroadcastReceiver {
    public SmsListener() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        //you code here
    }
}

现在,您可以在主要活动中注册接收器:

String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
SmsListener smsListener = new SmsListener();
registerReceiver(smsListener, new IntentFilter(SMS_RECEIVED));

请记住在最后取消注册。

当我卸载设备中的其他应用程序时,它也具有与我的应用程序相同的功能,例如hello 应用程序(在 Play 商店查看(。然后我安装了我的应用程序,它工作得很好。我认为另一个应用程序为新传入的短信设置优先级或其他事项。因此,由于这个原因,我们的应用程序广播接收器没有调用

将优先级更改为 1 使其对我有用。 它默认为 1000。
我还必须将名称从 .短信侦听器 到 .MainActivity$SmsListener,但这可能取决于你在哪里定义类(在我的例子中,它在MainActivity中(。

    <receiver android:name=".MainActivity$SmsListener"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="1">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

最新更新