【问题标题】:Android can't understand and make a SmsReceiver for KitKat devicesAndroid 无法理解并为 KitKat 设备制作 SmsReceiver
【发布时间】:2014-08-21 00:14:35
【问题描述】:

我关注了 DevBytes 的视频 - Android 4.4 SMS API's - http://www.youtube.com/watch?v=mdq0R2WQssQ

没有运气,我无法编写一个成功的应用程序来接收 kitkat 设备的 SMS 消息。

我制作了 DevBytes 开发人员在他的视频中所做的确切格式,当我在 DDMS + Debug 中发送消息时,我的接收器类没有跳转到断点。

清单:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name="com.example.kitkatreceiver.DefaultAppDeliver">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <action android:name="android.intent.action.SENDTO" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="sms" />
        <data android:scheme="smsto" />
        <data android:scheme="mms" />
        <data android:scheme="mmsto" />
    </intent-filter>

    </activity>

    <receiver android:name="com.example.kitkatreceiver.KitKatSmsReceiver"
              android:enabled="true"
              android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="1000">
            <action android:name="android.provider.Telephony.SMS_DELIVER"/>
        </intent-filter>
    </receiver>

</application>

接收者:

公共类 KitKatSmsReceiver 扩展 BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub

    for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(arg1)) {
        String messageBody = smsMessage.getMessageBody();
        Log.d("msg", messageBody);
    }

    Bundle b = arg1.getExtras();
    SmsMessage [] msgs;
    if(b != null) {
         Object[] pdus = (Object[]) b.get("pdus");
         msgs = new SmsMessage[pdus.length];
         for(int i=0; i<msgs.length; i++){
             msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
         }
         for(SmsMessage msg : msgs) {

         }
    }
}

}

另外我意识到你需要将我的应用设置为默认应用,如果我不这样做但仍然先接收短信而不是默认短信应用怎么办?

谢谢

【问题讨论】:

  • 来自文档:在 Android 4.4 上,只有一个应用可以接收新的 SMS_DELIVER_ACTION 意图,当新的 SMS 消息到达时系统会广播该意图。哪个应用接收此广播取决于用户在系统设置中选择了哪个应用作为默认短信应用。因此,除非您将您的应用设为默认应用,否则我认为您无法“先接收短信”。

标签: android sms broadcastreceiver android-4.4-kitkat


【解决方案1】:

所需权限:

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

另外,在&lt;application&gt; 标签之前添加权限,而不是在接收者内部。

清单中的 BroadcastReciever 应如下所示:

<receiver android:name="com.example.kitkatreceiver.KitKatSmsReceiver">  
    <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
相关资源
最近更新 更多