2

I am building a simple sms app , i want to open my app automaticaly when ever a new sms is received? is it even possible? i am using broadcast receiver for this

what changes should i made in manifest?

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

<uses-sdk android:minSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MessageActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".SmsReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </activity>
    <activity android:name=".Reply" >
    </activity>
 </application>

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

</manifest>
Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53
  • I think it should work if you are adding the receiver at the Manifest level and not at the Activity level(Using code). – Shashank Kadne Aug 12 '12 at 10:44
  • how exactly can i do that shashank?? i am a newbee in android – Mr_Hmp Aug 12 '12 at 16:51
  • I have this problem too. my receiver is out of activity level but when app is closed, it wont recived any thing after opened with notification. – Mahdi Feb 21 '14 at 11:11

3 Answers3

2

you should move the receiver outside from the activity like this:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MessageActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Reply" >
    </activity>
<receiver android:name=".SmsReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
 </application>
Tomer Mor
  • 7,998
  • 5
  • 29
  • 43
1

Under Android 3.0 and later, your broadcast is not guaranteed to be received unless the user has opened your application. The application does not have to remain opened, just to have been started once. This is caused by a flag (FLAG_EXCLUDE_STOPPED_PACKAGES) which is now part of most system broadcasts, which says the broadcast should not start a stopped application.

mah
  • 39,056
  • 9
  • 76
  • 93
0

This was already answered in How to make android launch an application on received specific sms to keep it short: it is possible with BroadcastReceiver.

Community
  • 1
  • 1
Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54