0

My app used to work when it was running in the background. The app simply sends a text message to a person whenever it receives a text message (essentially an away message).

With the new update they removed the permission BROADCAST_SMS which is what I think was allowing the app to operate in the background.

So my problem is this, I need my app to work when it is not displayed. In other words, My app currently works as expected when it is open and focused but as soon as the user minimizes/sends the app to the background, the app no longer works.

Here is the code behind my manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.katianie.awayreply2"
    android:versionCode="2"
    android:versionName="2.0" >

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:persistent="true" >
        <activity
            android:name="com.katianie.awayreply2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Here is how I am sending a sms text message:

public void sendTextMessage(String phoneNo, String message)
{
    //Used to detect when the sent text message was/was not delivered.
    String actionStr = "android.provider.Telephony.SMS_DELIVERED";
    Intent theDeliveredIntent = new Intent(actionStr);
    PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(myMainActivity, 0, theDeliveredIntent, 0);
    SmsManager smsManager = SmsManager.getDefault();

    //When the SMS has been delivered, Notify the user with a simple toast.
    myMainActivity.registerReceiver(new BroadcastReceiver() 
    {
        //Called when the away message has gotten to the destination.
        @Override
        public void onReceive(Context arg0, Intent arg1) 
        {
            int resultCode = getResultCode();

            if(resultCode == Activity.RESULT_OK)
            {
                Toast.makeText(myMainActivity.getBaseContext(), "Away message delivered.", Toast.LENGTH_SHORT).show();
            }
            else if(resultCode == Activity.RESULT_CANCELED)
            {
                Toast.makeText(myMainActivity.getBaseContext(), "Away message could NOT be delivered.", Toast.LENGTH_SHORT).show();
            }

            myHasSentMessage = false;

        }
    }, new IntentFilter(actionStr));

    //Send the text message
    if(!myHasSentMessage)
    {
        smsManager.sendTextMessage(phoneNo, null, message, deliveredPendingIntent, null);
    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Katianie
  • 589
  • 1
  • 9
  • 38
  • 1
    As mentioned in my comment on the now-deleted question, the `BROADCAST_SMS` permission is irrelevant. As for the code you've posted, you don't have a BroadcastReceiver statically registered in the manifest, so I'm not sure how reliably your app actually used to perform the described task. There are many examples on this site of how to register your Receiver in the manifest, so your app can be notified of SMS receipt, even if it's not running. Please consult one. You've got basically everything you need; you just need to shuffle some things around. – Mike M. Dec 10 '14 at 23:54
  • What android.intent.action would I use to accomplish what you are suggesting? Considering the messaging class extends BroadcastReceiver. – Katianie Dec 11 '14 at 02:47
  • 1
    Please check [this post](http://stackoverflow.com/questions/13890903/receiving-sms-in-android-manifest-xml). The question itself contains a basic SMS with the described Receiver. Ignore the answers. – Mike M. Dec 11 '14 at 08:33

0 Answers0