AndroidCB
AndroidCB

Reputation: 240

Recieving text messages within my app

The aim of my app is to grab in a text message and then display it on screen within a dialog box. It should also make it so that the message does not appear in the phones inbox. Currently i've coded what i think should work but when i send texts to the phone, they just appear in the inbox with no response from the app at all.

code below:

    public void onReceive(Context context, Intent intent) 
    {
     //---get the SMS message passed in---
     Bundle bundle = intent.getExtras();        
     SmsMessage[] msgs = null;
     String str = "";            
     if (bundle != null)
     {
     //---retrieve the SMS message received---
     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]);                
         str += "SMS from " + msgs[i].getOriginatingAddress();                     
         str += " :";
         str += msgs[i].getMessageBody().toString();
         str += "\n";        
         Intent act = new Intent(context, MainActivity.class);
         act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         act.putExtra("message",str);
         context.startActivity(act);
         //abortBroadcast();
     }
     //---display the new SMS message---
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
     }       


    }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cam.sms"
    android:versionCode="1"
    android:versionName="1.0" >
  <uses-permission android:name="android.permission.SEND_SMS" />
  <uses-permission android:name="android.permission.WRITE_SMS"/>
  <uses-permission android:name="android.permission.READ_SMS" />
  <uses-permission android:name="android.permission.RECIEVE_SMS"/>



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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cam.sms.MainActivity"
            android:label="@string/app_name"
            android:alwaysRetainTaskState="True"
            android:launchMode="singleInstance">"
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name=".SettingsScreen"
      android:alwaysRetainTaskState="True"
          android:launchMode="singleInstance">
</activity>


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


</application>

Upvotes: 1

Views: 282

Answers (3)

Paul Alexander
Paul Alexander

Reputation: 2748

add

<intent-filter android:priority="100">
<uses-permission android:name="android.permission.RECEIVE_SMS" />

to your manifest

Upvotes: 1

nanithehaddock
nanithehaddock

Reputation: 257

BroadcastReceiver is a service. which means that it always look for the service and it is independent to the app activity. so it won't show the application dependent Toast in service but you can call Activity using this service.

and for to receive first into your application give priority level 100 in your manifest file. use
abortBroadcast(); method in your code

Upvotes: 0

Aleksandar Panic
Aleksandar Panic

Reputation: 165

In Manifest add one row inside of Intent filter of your app:

<intent-filter android:priority="100">

It means that your app will first decide what to do with your message.

Also, add in your code

this.abortBroadcast();

to stop further forwarding.

EDIT: Try also adding

Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("SMSReceiver",str);

Upvotes: 0

Related Questions