BroadcastReceiver会工作一段时间,然后停止 [英] BroadcastReceiver works for a while, then stops
问题描述
我正在开发一个可响应来电的android应用.我已经设置了广播接收器,在清单文件中设置了权限和意图过滤器,并且在很多时候都可以使用.我可以杀死所有应用程序,重启手机,它仍然可以工作.
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.
我尝试修剪所有代码,并在接到或结束通话时仅显示Toast消息.即使该应用仅执行此操作,其行为仍然相同.
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.
我无法在模拟器上复制它,我正在使用 HUAWEI ASCEND Y550 进行设备测试.
I can't replicate this on an emulator, I'm using my HUAWEI ASCEND Y550 to device test.
我们非常感谢您的帮助,
Any help is hugely appreciated,
大卫
清单:
<?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>
我的接收器:
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)
{
}
}
}
}
推荐答案
我想我可能已经解决了这个问题-尽管我不熟悉开发甚至使用Android手机...
I think I may have solved this - although I'm new to developing and even using android phones...
在HUAWEI手机中,我的受保护的应用程序"设置中有一个选项-不会被杀死的应用程序.
In my HUAWEI phone, I have an option in my settings for "Protected Apps" - those which won't get killed.
我看过几个朋友的android手机,却无法找到相同的设置-所以我可以说这是HUAWEI特有的功能,可以弥补电池电量不足的问题.
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?
大卫
这篇关于BroadcastReceiver会工作一段时间,然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!