Search code examples
androidbroadcastreceiversms

BroadcastReceiver not working after 2 or 3 minute if clear app from recent


My app work perfect in Emulator . But if i clear it from recent in real device it stop working after 2 or 3 minute if i clear the app from recent ( i am not using any ram cleaning app )

Manifest.xml

    <receiver
        android:name="com.crezyprogrammer.mygoogle3.SMSReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="2147483647">
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter android:priority="999999999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />

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

SMSReciver.java

 public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        // boot-related processing here
        Toast.makeText(context, "boot completed", Toast.LENGTH_SHORT).show();

    }

    else if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        // SMS-related processing here

                Toast.makeText(context, "message recived", Toast.LENGTH_SHORT).show();
             }
        }

I also added those permission in manifest and runtime

 <uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Solution

  • Basically Android OS kills an application at several positions for several reasons. That's a lot of talks.

    Use some service along with the broadcast receiver. Write if the system kills the service another broadcast receiver will start the service again. By doing so your app will continue running no matter what does system/Android OS do with your app.