Search code examples
javaandroidbroadcastreceiver

Can't make a toast with a broadcastreceiver, never call onReceive


My question will be seem's stupid but I make a simple code to make a Toast when I receive a SMS, but impossible to see my toast.

My Android Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.romain.assistantvocal" >
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

    <receiver class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <activity
        android:name=".MyActivity"
        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 class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

And here my class SMSReceiver :

package com.example.romain.assistantvocal;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

import java.util.Locale;

/**
 * Created by romain on 26/09/2014.
 */
public class SMSReceiver extends BroadcastReceiver
{


    @Override
    public void onReceive(Context context, Intent intent)
    {

        Toast.makeText(context,"gg romstan",Toast.LENGTH_LONG).show();

    }

}

Thank you in advance for your answers.


Solution

  • replace
        <receiver class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
    with
            <receiver android:name="com.example.romain.assistantvocal.SMSReceiver">
    

    If ur aware of Ordered broadcasts then it says "As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. " So. If any broadcast has stopped passing data, u won't get the message. Try putting log in ur receiver and check. Whether ur receive message or not because ur toast message is correct. U can't keep priority more than 1000 as per android guidelines. so, give some thing near to 1000 or remove priority and check.