简体   繁体   中英

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. From intellij, using the run command, the APK is loaded to a phone running on 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.

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 {

/**
 * 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. If you need it to stay active you may need to look at using a WakeLock .

Be aware though that since this prevents the cpu from shutting off that it will cause faster battery drain. 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.

I don't think WakeLock s are the answer. I use BroadcastReceiver s constantly to receive Intents broadcast by the system and never have had that problem.

Not sure if this is your problem, but refer to this snippet from IntentFilter documentation

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

So maybe see what would happen if you try 999?

EDIT: alternatively, have you looked into 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. So if you are in fact having a wakelock problem, this would resolve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM