Emad
Emad

Reputation: 638

prevent a Broadcast Receiver from being killed by recent apps

In my app,I'm using a Broadcast Receiver to detect arrived sms messages...
when my app is running,works well...
but after killed by recent apps,when an sms with MessageBody:"silent" arrived ,my app crashes...!
// silent is an action in my SMS BroadcastReceiver ...

Here is my code:

Manifest:

<receiver android:name=".SmsReceiver" >
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

BroadcastReceiver:

public class SmsReceiver extends BroadcastReceiver {
public static String senderTel;
public static String messageBody;




    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");

        SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
        senderTel = sms.getOriginatingAddress();
        messageBody = sms.getMessageBody().toLowerCase().trim();




        if(messageBody.equals("silent")){
            G.AudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            G.AudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Toast.makeText(G.currentActivity, "silent", Toast.LENGTH_SHORT).show();
        }
    }

G :

public class G extends Application {

    public static Context           context;
    public static Activity currentActivity;

 @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        AudioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
    }
}

LogCat:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

So I tried to use service and alarm manager but they failed

Intent intent = new Intent(G.context, SmsReceiver.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        G.alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 3000, pendingIntent);

I want to keep my broadcast receiver alive even after the app kill by recent apps or force close...
Is it necessary to use Service ...?


Thank you for any help you can provide

Upvotes: 1

Views: 2622

Answers (2)

RobVoisey
RobVoisey

Reputation: 1083

Try changing:

Toast.makeText(G.currentActivity, "silent", Toast.LENGTH_SHORT).show();

to

Toast.makeText(context, "silent", Toast.LENGTH_SHORT).show();

The application is being killed, not the BroadcastReceiver. The receiver is invoked when it is needed by android. The solution here is to use the context that is provided by the method parameters instead of the activity which is possible to be null.

If you wanted to only show the toast when the app is opened, consider adding a check to see if currentActivity is null (assuming it is assigned onStart and cleaned onStop), or registering the receiver in the activity.

Upvotes: 3

Mateus Brandao
Mateus Brandao

Reputation: 900

I believe the main issue here is that in your BroadcastReceiver, you are calling a reference from your application (G.currentActivity), and since the application is dead, that makes the BroadcastReceivercrash. The problem is not that your BroadcastReceiver is not working...

Upvotes: 3

Related Questions