[英]BroadcastReceiver stops working after few hours the phone was locked
I have already searched for solutions to this issue but I was not able to find one. 我已经在寻找解决此问题的方法,但是找不到。 Let me discuss the issue first. 让我先讨论这个问题。
I've got a broadcast receiver that was created using Intellij. 我有一个使用Intellij创建的广播接收器。 From intellij, using the run command, the APK is loaded to a phone running on android 2.2.1. 从intellij,使用run命令,将APK加载到在Android 2.2.1上运行的手机。 At first the broadcast receiver works well but when the phone is locked, after few hours the broadcastreceiver seems to stop working. 起初,广播接收器工作良好,但是当电话锁定后,几个小时后广播接收器似乎停止工作。
I think I have configured my androidmanifest.xml and the code is okay as well since it is working before the phone is locked and when the phone is unlocked. 我想我已经配置了androidmanifest.xml,代码也可以,因为它在手机锁定之前和手机解锁时都可以正常工作。
Anyway, below are the snippet for the involve code for this. 无论如何,下面是涉及此代码的代码段。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.smsapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application android:label="@string/app_name">
<receiver android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".SMSApp"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the broadcast receiver snippet: 这是广播接收器片段:
public class SmsReceiver extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
String textMessage = getReceivedMessage(context, intent);
SharedPreferences pref = context.getSharedPreferences("smsapp", context.MODE_PRIVATE);
try {
GMailSender sender = new GMailSender("test@email.com", "pass");
sender.sendMail("New Message Received",
getReceivedMessage(context, intent),
"test@email.com",
"recvr@email.com");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
} }
public class SMSApp extends Activity { 公共类SMSApp扩展了活动{
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
} }
Please let me know if what could be the possible cause of this issue and what are the steps I can probably take so this will be fixed. 请让我知道是否可能导致此问题的原因以及我可能采取的步骤,以便将其解决。
Thanks!!! 谢谢!!!
Edit: Just to add up, I tried using the emulator but it works well... 编辑:只是加起来,我尝试使用模拟器,但效果很好...
您可以尝试使用Asynctask() doinBackground()函数将代码逻辑置于后台。
What may be happening is that since the phone has been locked for a while it has gone into deep sleep mode and the cpu has been shut down. 可能发生的情况是,由于手机已被锁定一段时间,因此进入了深度睡眠模式,并且cpu已关闭。 If you need it to stay active you may need to look at using a WakeLock . 如果您需要它保持活动状态,则可能需要使用WakeLock进行检查 。
Be aware though that since this prevents the cpu from shutting off that it will cause faster battery drain. 但是请注意,由于这会阻止CPU关闭,因此会导致电池消耗更快。 So, you could make it a user option whether or not it is enabled. 因此,无论是否启用它,您都可以将其设为用户选项。
Another possibility for you to do some work at a future time without requiring a WakeLock is to use the AlarmManager , but this only is useful if you want to do something at some known time in the future. 您将来不需要WakeLock进行某些工作的另一种可能性是使用AlarmManager ,但这仅在您想在将来某个已知时间做某事时有用。
I don't think WakeLock
s are the answer. 我不认为WakeLock
是答案。 I use BroadcastReceiver
s constantly to receive Intents broadcast by the system and never have had that problem. 我经常使用BroadcastReceiver
来接收系统BroadcastReceiver
的Intent,从来没有遇到过这个问题。
Not sure if this is your problem, but refer to this snippet from IntentFilter
documentation 不确定这是否是您的问题,但请参考IntentFilter
文档中的此代码段
The value must be an integer, such as "100". 该值必须是整数,例如“ 100”。 Higher numbers have a higher priority. 数字越高优先级越高。 The default value is 0. The value must be greater than -1000 and less than 1000. 缺省值为0。该值必须大于-1000并且小于1000。
So maybe see what would happen if you try 999? 所以也许看看如果您尝试999会发生什么?
EDIT: alternatively, have you looked into WakefulBroadcastReceivers
? 编辑:或者,您是否看过WakefulBroadcastReceivers
? This combines the BroadcastReceiver
with a partial wakelock in order to make sure all of your activity is handled before the CPU goes back to sleep. 这将BroadcastReceiver
与部分唤醒锁结合在一起,以确保在CPU再次进入睡眠状态之前已处理所有活动。 So if you are in fact having a wakelock problem, this would resolve it. 因此,如果您实际上遇到了唤醒锁问题,则可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.