短信广播接收器重启后无法正常工作

时间:2015-09-01 21:05:56

标签: android service sms broadcastreceiver

我正在尝试从短信中获取电话号码和电话簿名称。当我从应用程序运行它并关闭应用程序时,它可以工作,但是,当我重新启动我的手机时,它不起作用。任何人

public class IncomingSMSReceiver extends BroadcastReceiver {
    private static final String queryString = "@zovi";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    public void onReceive(Context _context, Intent _intent) {
        if (_intent.getAction().equals(SMS_RECEIVED)) {
            Intent intent = new Intent(_context, IncomingSMSService.class);
            _context.startService(intent);
            Bundle bundle = _intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                for (SmsMessage message : messages) {
                    String msg = message.getMessageBody();
                    Log.i("Poruka", msg);
                    String to = message.getOriginatingAddress();
                    String contactName = TelefonUtils.getContact(_context, to);
                    Log.i("Od", contactName + "\n" + to);
                }
            }
        }
    }
}

我的XML:

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
          android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="500">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

6 个答案:

答案 0 :(得分:9)

AndroidManifest.xml尝试在<action android:name="android.intent.action.BOOT_COMPLETED" />收件人代码下添加IncomingSMSReceiver操作。

要在Android系统重新启动或启动后自动启动ServicesBroadcastReceiver,您可以将BroadcastReceiver注册到Android android.intent.action.BOOT_COMPLETED系统事件。

试试这段代码。

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
 android:permission="android.permission.BROADCAST_SMS">
       <intent-filter android:priority="500">
       <action android:name="android.provider.Telephony.SMS_RECEIVED" />
       <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

如果您已尝试android.intent.action.BOOT_COMPLETED,那么在您的情况下它将无法正常工作,因为您忘记在其中添加BOOT_COMPLETED意图 IncomingSMSReceiver只有在android.provider.Telephony.SMS_RECEIVED的情况下它才有效,因为您在IncomingSMSReceiver中使用了if条件。所以如果条件改变

if (_intent.getAction().equals(SMS_RECEIVED)) {

if (_intent.getAction().equals(SMS_RECEIVED) || _intent.getAction().equals(BOOT_COMPLETED)) {

并在private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";中定义IncomingSMSReceiver。以下是IncomingSMSReceiver的完整代码。

将您的IncomingSMSReceiver代码更改为:

public class IncomingSMSReceiver extends BroadcastReceiver {
    private static final String queryString = "@zovi";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";

    public void onReceive(Context _context, Intent _intent) {
        if (_intent.getAction().equals(SMS_RECEIVED) || _intent.getAction().equals(BOOT_COMPLETED)) {
            Intent intent = new Intent(_context, IncomingSMSService.class);
            _context.startService(intent);
            Bundle bundle = _intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++)
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                for (SmsMessage message : messages) {
                    String msg = message.getMessageBody();
                    Log.i("Poruka", msg);
                    String to = message.getOriginatingAddress();
                    String contactName = TelefonUtils.getContact(_context, to);
                    Log.i("Od", contactName + "\n" + to);
                }
            }
        }
    }
}

因此,当您的手机重新启动时,它会收到android.intent.action.BOOT_COMPLETED并致电您的IncomingSMSReceiver接收器,然后它会启动您的IncomingSMSService

我希望它会对你有所帮助。

答案 1 :(得分:3)

Make sure that the receiver starts after phone reboots.

Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in intent-filter

答案 2 :(得分:1)

Ideally it should have worked.Is weird that its not working.

Remove android:permission="android.permission.BROADCAST_SMS" from the broadcast receiver.It is not required.

Try increasing the priority android:priority="2147483647" to maximum.It might be some other app is consuming the event or try the below solution.

Add <action android:name="android.intent.action.BOOT_COMPLETED" /> in your intent filter and see if it works.

Something like this:

<receiver android:name=".telefon.receivers.IncomingSMSReceiver"
          android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="500">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

and add permission:

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

答案 3 :(得分:1)

你必须删除android:permission =&#34; android.permission.BROADCAST_SMS&#34;来自接收者声明。不需要BOOT_COMPLETED或其他许可。

答案 4 :(得分:1)

将您的清单中的exported明确设置为true,作为Receiver

的元素
 android:exported="true"

并确保启用了应用程序元素和接收者元素

android:enabled="true"

答案 5 :(得分:1)

广播代码似乎没问题,请确保您在设备中使用任何其他短信应用(可能此短信广播可能会中断您的应用广播)。如果您没有使用其他短信应用程序,那么它应该工作,否则取消安装该SMS应用程序并尝试一次。

并检查您是否在AndroidManifest.xml中添加了以下权限

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

希望这会奏效。