0

I am new to android and I am trying to learn by doing some basics projects. The current app I am working on requires me to use BroadcastReceiver, but I need it to run when the phone starts and when the app closes.

I need it to receive all the incoming SMS, even when the app is not running.

Currently I have a startForeground which uses a notification to run. Is there a way to make the app run without the notification?

Please help. Here is my code:

public class IncomingSMS extends Service {
    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    //get audioManager
   AudioManager aud;

    public void onCreate(){
        Log.i("MyActivity",  "aaa");
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);



        registerReceiver(receiver, filter);

registerReceiver(receiver, filter);
    Notification notification= new Notification();
    startForeground(1, notification);


    }

    public void onDestroy(){
        unregisterReceiver(receiver);
        Log.i("MyActivity",  "Destroyed");
    }
    public int onStartCommand(Intent intent, int flags, int startId){
        return START_STICKY;
    }


    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Retrieves a map of extended data from the intent.
            Log.i("MyActivity",  "aaa");
            final Bundle bundle = intent.getExtras();
            aud = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

            try {


                if (bundle != null) {
                    Log.i("MyActivity",  "Bundle");

                    final Object[] pdusObj = (Object[]) bundle.get("pdus");

                    for (int i = 0; i < pdusObj.length; i++) {

                        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

                        String message = currentMessage.getDisplayMessageBody();
                        Log.i("MyActivity",  message);
                         //Some Code



                    } // end for loop
                } // bundle is null




            }catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" + e);
                Log.i("MyActivity",  "bbb");
            }
        }
    };


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

And AndroidManifest.xml:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pi.sum.nesbtesh.SARA" >

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <service android:name="......">
           <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
               <action android:name="android.provider.Telephony.SMS_RECEIVED" />
           </intent-filter>
        </service>


        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
user3527318
  • 143
  • 1
  • 10

1 Answers1

0

What you want is a BroadcastRceiever to run on phone startup, here you go. Basically you start your service in its callback.

For when the app closes, I wouldn't say that's quite possible because most of the times when you close an app, it just stats in Stopped but isn't destroyed. So I'd suggest you create a BaseActivity which is an Activity subclass that override onStop() or onPause() and start your service in this method.

Then every other Activity in your app are subclass of BaseActivity

Community
  • 1
  • 1
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • Hi Christopher, thank you for your answer, BroadcastReciver stops running after a few minutes of startup. – user3527318 Feb 08 '15 at 18:42
  • 1
    BroadcastReceiver isn't supposed to run undefinitely. It just receive a broadcast from the android system and execute code. Then, in that code you start up your Service. – Christopher Francisco Feb 08 '15 at 22:02