Android 手机处于睡眠状态时接收短信

Android 手机处于睡眠状态时接收短信,android,Android,我有一个应用程序可以接收短信(通过BroadCastReceiver),并启动一个活动来处理传入的短信(包括播放声音通知用户、更新数据库和向用户显示消息) 问题是,有时应用程序没有运行,手机处于休眠状态,我什么也没收到,但一旦我解锁屏幕,我就收到了4到5条消息 编辑: 广播接收机的定义: <receiver android:name=".util.sms.SmsReceiver" android:enabled="true" >

我有一个应用程序可以接收短信(通过BroadCastReceiver),并启动一个活动来处理传入的短信(包括播放声音通知用户、更新数据库和向用户显示消息)

问题是,有时应用程序没有运行,手机处于休眠状态,我什么也没收到,但一旦我解锁屏幕,我就收到了4到5条消息

编辑: 广播接收机的定义:

    <receiver
        android:name=".util.sms.SmsReceiver"
        android:enabled="true" >
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
            <data android:scheme="sms" />
            <data android:port="56790" />
        </intent-filter>
    </receiver>
@Override
public void onReceive(Context context, Intent intent) {
        //...
        if( smsIsMine() ) {
    Intent intent = new Intent();
    intent.putExtra("MESSAGE_BODY", sms.getBody());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClass(context, MyActivity.class);
    context.startActivity(intent);                
        }
    }
正在从BroadcastReceiver启动MyActivity:

    <receiver
        android:name=".util.sms.SmsReceiver"
        android:enabled="true" >
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
            <data android:scheme="sms" />
            <data android:port="56790" />
        </intent-filter>
    </receiver>
@Override
public void onReceive(Context context, Intent intent) {
        //...
        if( smsIsMine() ) {
    Intent intent = new Intent();
    intent.putExtra("MESSAGE_BODY", sms.getBody());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClass(context, MyActivity.class);
    context.startActivity(intent);                
        }
    }

有什么想法吗?

在清单中注册你的接收者。像这样:

<receiver android:name="com.app.SMSReceiver" android:enabled="true"> 
        <intent-filter android:priority="999"> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
        </intent-filter> 
    </receiver>

解决方案是使用WakeLock,如下所示“


我这样做了,我正在测试。您能告诉我更多关于如何解决问题的详细信息吗?我需要查看您的代码。我假设您正在从活动内部注册接收器。当您这样做时,该广播接收器会在活动关闭或进入onPause状态时自动取消注册。当您在m中注册时anifest,无论应用程序状态如何(打开、关闭、后台),广播接收器都会接收到意图并执行广播接收器的onReceive()。我在清单中注册接收器(就像你一样)但有时我解释过的事情发生了,我认为android本身有问题,比如在空闲模式下睡了一段时间后方法您没有在意向中设置任何操作。这是预期的吗?我只是启动了一个活动。问题就在那里!有时我收到3 4条短信,但活动没有按预期启动,当我解锁手机(或接听电话)时,所有这些短信一起触发!
@Override
public void onReceive(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager)                             context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();
    // your code ...
    wl.release();
}