广播接收器不处理短信的 [英] Broadcast Receiver not processing SMS's
问题描述
我已经看到了,这样@Commonsware指出,所有需要接收短信的是一个广播接收器(即的没有的使用服务)。我想采用这种方法,因为它是有道理的我。不幸的是它不会出现仿佛正在接收的SMS的/处理在我的广播接收器。将APK正在安装成功但,当我发送测试短信的生成没有日志条目。我究竟做错了什么?
这里的清单:
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=net.oheller.pete2
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =10
机器人:targetSdkVersion =17/>
<使用-权限的Android:名称=android.permission.SEND_SMS/>
<使用-权限的Android:名称=android.permission.RECEIVE_SMS/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ icon_broadcastreceiver
机器人:标签=@字符串/ sms_broadcastreceiver
机器人:主题=@风格/ Theme.eagleeyeactionbar>
<接收机器人:名称=。SMSReceiver_Test>
<意图过滤器的Android版本:优先=999>
<作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
&所述; /意图滤光器>
< /接收器>
<供应商
机器人:名称=。contentprovider.GPSTrackerContentProvider
机器人:当局=net.oheller.pete2.contentprovider
机器人:grantUriPermissions =真
机器人:多进程=真
机器人:权限=真正的>
< /供应商>
< /用途>
< /舱单>
下面是短信接收器code。它的目的是通过短信的收集数据,写每一条记录的内容提供商(SQLite数据库)。
公共类SMSReceiver_Test扩展的BroadcastReceiver {
TelephonyManager telephonyManager;
字符串deviceCountry code =;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
//从意图通过在SMS消息
捆绑SMSmessageContent = intent.getExtras();
SmsMessage [] receivedSMS = NULL;
字符串returnStr =;
龙current_time_stamp = Calendar.getInstance()getTimeInMillis()。
字符串curTimeString = getFormattedDate(current_time_stamp,DD-MMM-YY H:MMA);
telephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
字符串incoming_SMSphone_number =;
字符串SMSmsgText =;
长的timeStamp = current_time_stamp;
如果(SMSmessageContent!= NULL){// [IF有效短信]
//获得的短信信息
[对象]的PDU =(对象[])SMSmessageContent.get(的PDU);
receivedSMS =新SmsMessage [pdus.length]
的for(int i = 0; I< receivedSMS.length;我++){// [FOR得到短信内容]
receivedSMS [I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
。incoming_SMSphone_number = receivedSMS [I] .getOriginatingAddress()的toString()修剪();
。SMSmsgText = receivedSMS [I] .getMessageBody()的toString();
returnStr + = curTimeString +\ N的;
Log.d(鹰眼的String.Format(SMSReciever:短信= \%s \的,SMSmsgText)); /// DEBUG
} // [ENDFOR得到短信内容]
///////////////////////////
// prepare值用于插入SQLite的数据库
ContentValues GPSTrackValues =新ContentValues();
GPSTrackValues.put(GPSTrackerTable.COLUMN_PHONE_NUMBER,incoming_SMSphone_number);
GPSTrackValues.put(GPSTrackerTable.COLUMN_TIME_STAMP,时间戳);
//// prepare访问内容提供商
乌里GPSuri = GPSTrackerContentProvider.CONTENT_URI_GPS_Tracks; //指定内容提供商的位置
ContentResolver的GPSTrackCR = context.getContentResolver();
//插入新进入数据库
乌里insertURI = GPSTrackCR.insert(GPSuri,GPSTrackValues);
Log.d(鹰眼,数据块插入的结果=+ insertURI); //////// DEBUG
} // [ENDIF有效短信]
} // [结束的onReceive]
} // [结束SMSReceiver]
code在我看来,正确的。 您可以通过增加最多的优先尝试。
<意图过滤器的Android版本:优先=2147483647>
有可能的情况下,当其他接收器之前得到你的短信和中止它们。
I've seen on SO that @Commonsware states that all that is needed to receive SMS's is a Broadcast Receiver (i.e., without the use of a service). I'd like to employ that approach since it makes sense to me. Unfortunately it does not appear as if SMS's are being received/processed in my Broadcast Receiver. The apk is being installed successfully but no log entries are being generated when I send test SMS's. What am I doing wrong?
Here's the Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.oheller.pete2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/icon_broadcastreceiver"
android:label="@string/sms_broadcastreceiver"
android:theme="@style/Theme.eagleeyeactionbar" >
<receiver android:name=".SMSReceiver_Test" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<provider
android:name=".contentprovider.GPSTrackerContentProvider"
android:authorities="net.oheller.pete2.contentprovider"
android:grantUriPermissions="true"
android:multiprocess="true"
android:permission="true" >
</provider>
</application>
</manifest>
Here's the SMS Receiver code. It's purpose is to collect data via SMS's and write a record of each to the content provider (SQLite database).
public class SMSReceiver_Test extends BroadcastReceiver {
TelephonyManager telephonyManager;
String deviceCountryCode = "";
@Override
public void onReceive(Context context, Intent intent) {
//Get the SMS message passed in from the intent
Bundle SMSmessageContent = intent.getExtras();
SmsMessage[] receivedSMS = null;
String returnStr = "";
Long current_time_stamp = Calendar.getInstance().getTimeInMillis();
String curTimeString = getFormattedDate(current_time_stamp, "dd-MMM-yy h:mma");
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String incoming_SMSphone_number = "";
String SMSmsgText = "";
long timeStamp = current_time_stamp;
if (SMSmessageContent != null) { // [IF valid SMS]
//Retrieve the SMS message info
Object[] pdus = (Object[]) SMSmessageContent.get("pdus");
receivedSMS = new SmsMessage[pdus.length];
for (int i=0; i<receivedSMS.length; i++) { // [FOR get SMS content]
receivedSMS[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
incoming_SMSphone_number = receivedSMS[i].getOriginatingAddress().toString().trim();
SMSmsgText = receivedSMS[i].getMessageBody().toString();
returnStr += curTimeString +"\n";
Log.d("EagleEye", String.format("SMSReciever: SMS=\"%s\"", SMSmsgText)) ; ///DEBUG
} // [ENDFOR get SMS content]
///////////////////////////
// prepare values for insertion into the SQLite DB
ContentValues GPSTrackValues = new ContentValues();
GPSTrackValues.put(GPSTrackerTable.COLUMN_PHONE_NUMBER, incoming_SMSphone_number);
GPSTrackValues.put(GPSTrackerTable.COLUMN_TIME_STAMP, timeStamp);
//// Prepare access to content provider
Uri GPSuri = GPSTrackerContentProvider.CONTENT_URI_GPS_Tracks; //specify the content provider location
ContentResolver GPSTrackCR = context.getContentResolver();
// Insert new entry into DB
Uri insertURI = GPSTrackCR.insert(GPSuri, GPSTrackValues);
Log.d("EagleEye", "DB insert results="+insertURI) ; ////////DEBUG
} // [ENDIF Valid SMS]
} // [END onReceive]
} // [END SMSReceiver]
Code seems to me correct. You can try by increasing the priority by max ..
<intent-filter android:priority="2147483647">
There may be case , when other receiver is getting sms before yours and aborting them.
这篇关于广播接收器不处理短信的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!