我正在尝试从文本消息中获取电话号码和电话簿名称。当我从应用程序运行它并关闭应用程序时,它可以正常工作,但是当我重新启动手机时,它就不起作用了。有人知道原因吗?
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>
<action android:name="android.intent.action.BOOT_COMPLETED" />
。 - Moinkhan