我搜索过,搜索过,找不到答案。 我正在尝试做一个简单的SMS意图过滤器,当我在AVD上运行它时效果很好,但是当我在实际设备上调试时,接收器永远不会触发。我已经以各种方式修改了我的清单,无法让它工作。
任何人都可以找到丢失或不正确的内容吗?
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customs.dbc.smsrelay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.customs.dbc.smsrelay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.customs.dbc.smsrelay.StartServices" />
<service android:name="com.customs.dbc.smsrelay.SMSRelayMonitor" />
<receiver
android:name=".GotBoot"
android:enabled="true"
android:exported="true"
android:label="GotBoot"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name="com.customs.dbc.smsrelay.IncomingSms" >
<intent-filter
android:priority="999"
android:exported="true" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
接收器:
package com.customs.dbc.smsrelay;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
// got the message
//TODO This is where I can send the message off from here
Intent newSms = new Intent("SMSRELAYGOTNEWMESSAGE").putExtra("msg", message).putExtra("sender", senderNum);
context.sendBroadcast(newSms);
// Log it for debugging
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
更新
我现在也试图动态注册接收器,但仍然没有。
这是我的服务类:
package com.customs.dbc.smsrelay;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSRelayMonitor extends IntentService {
public SMSRelayMonitor() {
super("SMSRelayMonitor");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter("SMSRELAYGOTNEWMESSAGE");
this.registerReceiver(mIntentReceiver, filter);
IntentFilter smsFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(smsReceiver, smsFilter);
return START_STICKY;
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mIntentReceiver);
unregisterReceiver(smsReceiver);
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@SuppressWarnings("unused")
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("msg");
String sender = intent.getStringExtra("sender");
Notification notice = new Notification.Builder(context).setContentTitle("New message").build();
NotificationManager noticeManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notice.flags |= Notification.FLAG_AUTO_CANCEL;
noticeManager.notify(0, notice);
}
};
private final BroadcastReceiver smsReceiver = new BroadcastReceiver() {
//final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
// got the message
//TODO This is where I can send the message off from here
Intent newSms = new Intent("SMSRELAYGOTNEWMESSAGE").putExtra("msg", message).putExtra("sender", senderNum);
context.sendBroadcast(newSms);
// Log it for debugging
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
};
}
答案 0 :(得分:0)
我看到的问题是由手机上安装的另一个应用程序引起的。 “Go SMS Pro”是罪魁祸首。在我的硬件设备上进行出厂恢复后,意图就开始了。在我再次安装Go SMS Pro后,意图不会在我的应用程序上启动。