我需要捕获正在Android手机中发送的短信并将其记录到某个地方。到目前为止,我的尝试似乎没有奏效。 这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.secure.sms"
android:versionCode="1"
android:versionName="0.1" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="org.secure.sms.SecureMessagesActivity"
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=".MyAlarmService" />
<receiver
android:name="org.secure.sms.SmsReceiver"
android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver android:name="org.secure.sms.ServiceReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<activity
android:name="org.secure.sms.MyAlarmService"
android:label="@string/title_activity_my_alarm_service" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="org.secure.sms.SecureMessagesActivity" />
</activity>
</application>
</manifest>
这是我的观察员班级
package org.secure.sms;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
public class ServiceObserver extends ContentObserver {
private Context mContext;
//private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms/out");
public ServiceObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
public boolean deliverSelfNotifications() {
return true;
}
public void onChange(boolean selfChange) {
try{
//Log.e("Info","Notification on SMS observer");
Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
if (sms_sent_cursor != null) {
if (sms_sent_cursor.moveToFirst()) {
String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
Log.e("Info","protocol : " + protocol);
int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type"));
Log.e("Info","SMS Type : " + type);
// for actual state type=2
if(type == 2){
smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));
Log.e("Info","SMS Content : "+smsBodyStr);
Log.e("Info","SMS Phone No : "+phoneNoStr);
Log.e("Info","SMS Time : "+smsDatTime);
}
}
}
else
Log.e("Info","Send Cursor is Empty");
}
catch(Exception sggh){
Log.e("Error", "Error on onChange : "+sggh.toString());
}{
super.onChange(selfChange);
}
}
}
这是观察员注册
final Uri SMS_STATUS_URI = Uri.parse("content://sms/out");
ServiceObserver smsSentObserver = new ServiceObserver(new Handler(), this);
this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
编辑: 该应用程序现在工作正常,但我只能在我只访问应用程序时捕获短信,虽然Android显示在后台运行的进程和服务。这是我暂停的代码
我已经注册了一项服务并暂停了这段代码,但是如果手机响了,它会说一个服务和一个进程正在运行,但除非我访问应用程序,否则不会捕获短信。
protected void onPause() {
if(!TrackerService.isRunning){
serviceIntent = new Intent(MainActivity.this, TrackerService.class);
startService(serviceIntent);
mServiceReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TrackerService.mAction);
registerReceiver(mServiceReceiver, intentFilter);
}
super.onPause();
}
答案 0 :(得分:0)
尝试使用此
final Uri SMS_STATUS_URI = Uri.parse("content://sms");
答案 1 :(得分:0)
我遇到了同样的问题,似乎是一个Android错误。我创立的解决方案是here。首先你必须使用:
final Uri SMS_STATUS_URI = Uri.parse("content://sms");
这将收到发送和接收的短信,以了解您必须使用此代码中的哪一个:
注册观察员:
contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"), true, new mObserver(new Handler()));
观察者本身:
class mObserver extends ContentObserver {
// Constructor
public mObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("SMSTracker","onChange()");
// Uri to get all the SMS (sent and received)
Uri uriSMS = Uri.parse("content://sms");
Cursor cur = contentResolver.query(uriSMS, null, null, null, null);
cur.moveToNext();
// Debug
final String[] names = cur.getColumnNames();
for(int n = 0; n < cur.getColumnNames().length; ++n){
Log.i("SMSTracker", "Column[" + n + "] = " + names[n] + " --> " + cur.getString(cur.getColumnIndex(names[n])));
}
Log.i("SMSTracker", "SMS length: " + cur.getString(cur.getColumnIndex("body")).length() );
final int type = cur.getInt(cur.getColumnIndex("type"));
// type == 2 means a successfully sent message and type == 1 means a received message
if( (type == 2 || type == 1) && (!lastID.contentEquals(cur.getString(cur.getColumnIndex("_id")))) ){
String protocol = cur.getString(cur.getColumnIndex("protocol"));
String smsString = cur.getString(cur.getColumnIndex("body"));
lastID = cur.getString(cur.getColumnIndex("_id"));
// Sent SMS
if(protocol == null){
}
// Received SMS
else{
}
}
}
}
当protocol == null时,您已发送短信,否则您已收到短信。 (type == 2 || type == 1)
是因为短信似乎是分阶段发送的,当发送或接收短信类型== 1或类型== 2时。一切都是基于我的经验,希望他们可以帮助你!