Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver works for a while, then stops

I'm developing an android app that responds to incoming calls. I've set up a broadcast receiver, set the permission and intent filter in the manifest file, and a lot of the time, it works. I can kill all apps, reboot the phone and it still works..

However, sometimes it doesn't react, and after this happens once, it doesn't respond after a reboot either.

I've been struggling with this for about a month and it's driving me bananas. I really need my app to respond every time an incoming call is received.

I've tried trimming out all code, and just displaying a Toast message when a call is received or ends. Even when the app does nothing else but this, the behaviour is still the same.

I can't replicate this on an emulator, I'm using my HUAWEI ASCEND Y550 to device test.

Any help is hugely appreciated,

David

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.test.app.receivertest" >

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>
    <receiver android:name=".CallReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
  </application>
</manifest>

My Receiver:

public class CallReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    try {
        if (context == null && intent == null)
            return;

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        String action = intent.getAction();
        Toast.makeText(context, action + " " + state, Toast.LENGTH_SHORT).show();
    } catch (Exception ex) {
        try {
            Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
        }
    }
  }
}
like image 434
David McEleney Avatar asked Feb 07 '15 02:02

David McEleney


1 Answers

I think I may have solved this - although I'm new to developing and even using android phones...

In my HUAWEI phone, I have an option in my settings for "Protected Apps" - those which won't get killed.

I've had a look at a few friends android phones and have been unable to locate the same settings - So am I right to say it's a HUAWEI specific feature that compensates for a poor battery.

Can anyone confirm this is a HUAWEI specific issue?

David

like image 114
David McEleney Avatar answered Sep 29 '22 23:09

David McEleney