Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

android - 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)
        {
        }
    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...