android - 无法通过此代码接收消息(广播接收器)

标签 android broadcastreceiver

我没有通过我的应用程序收到消息 - 帮助我,我不知道我必须在此代码中进行哪些更改,请告诉我我应该将其设置为默认值(这样它会给我一条消息)还是在我的代码 - 我确实收到了一条消息,但不是来自这个应用程序

    public class IncomingSms extends BroadcastReceiver {

    final SmsManager sms = SmsManager.getDefault();

    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);

        }
    }

}

list .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>
</application>

最佳答案

我认为,你应该添加这段代码

<receiver android:name=".yourService" android:exported="true" > 
  <intent-filter> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-filter> 
</receiver>

在 list 中的应用程序标签中

或者您必须在 Activity 类中注册您的 InComingservice。

例如

private InComingSMSReceiver inComingSMSReceiver = new InComingSMSReceiver();
@Override
   protected void onResume() {
      super.onResume();
      registerReceiver(inComingSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
   }

   @Override
   protected void onPause() {
      super.onPause();
      unregisterReceiver(inComingSMSReceiver);
   }

关于android - 无法通过此代码接收消息(广播接收器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29532413/

相关文章:

android - 如何在 android studio 中禁用 gradle 'offline mode'?

java - 运行 google map api v2 时出错

android - onCreate 中的 moveTaskToBack 导致 Activity 短暂显示然后隐藏

android - 为什么要在 BroadcastReceiver 中调用 setResult?

android - 检测拨出电话,真实设备问题

android - 创建包含 TimePicker 和 DatePicker 的对话框时出现问题?

android - 一个应用两套资源

Android ICS API 14 - Camera.Face 人脸识别

android - 如何触发fragment的onCreate方法?

android - 应用程序强制关闭时未执行广播接收器