Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid BroadcastReceiver don't work with android 4.x but work with emulator
    primarykey
    data
    text
    <p>For my project, I need to use an BroadcastReceiver to receive the SMS. My code work with my emulator but no with my phone. My phone is a HTC one X (android 4.0.3). In the emulation, I can see the toast when I receive an SMS and in my phone I don't see that...</p> <p>My code:</p> <p>SMSReceiver.java:</p> <pre><code>package com.tuto.smsreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class SMSReceiver extends BroadcastReceiver { private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_RECEIVE_SMS)) { Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); final SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i &lt; pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } if (messages.length &gt; -1) { final String messageBody = messages[0].getMessageBody(); final String phoneNumber = messages[0].getDisplayOriginatingAddress(); Toast.makeText(context, "Exp : " + phoneNumber, Toast.LENGTH_LONG).show(); Toast.makeText(context, "MSG recu : " + messageBody, Toast.LENGTH_LONG).show(); } } } } } </code></pre> <p>AndroidManifest.xml:</p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tuto.smsreceiver" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;receiver class="com.tuto.smsreceiver.SMSReceiver" android:name="com.tuto.smsreceiver.SMSReceiver"&gt; &lt;intent-filter android:priority="999"&gt; &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt; &lt;uses-permission android:name="android.permission.READ_SMS" /&gt; &lt;/manifest&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload