应用关闭时接收短信

时间:2015-05-17 18:10:00

标签: android android-intent sms broadcastreceiver receiver

关闭应用后如何接收短信? 以下代码工作正常,但当应用关闭时,例如重启后,它不起作用。 (实际上,它在应用程序关闭后的前几分钟工作,奇怪......)

我的AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
    <activity ....>
    ....
    </activity>

    <receiver android:name=".Receiver" android:exported="true" android:enabled="true">
        <intent-filter android:priority="1">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

我的Receiver.java

public class Receiver extends BroadcastReceiver
{
    @Override public void onReceive(Context context, Intent intent)
    {
        if(cond)
        {
            abortBroadcast();
            Toast.makeText(context, "Registration Completed", Toast.LENGTH_LONG).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想在应用关闭后继续接收短信,您应该实施所有在服务中接收短信的内容,这些内容将不会与您的应用关闭。请参阅此示例:

Having a Service receive SMS messages