权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
清单文件注册
<!--短信和电话监听服务-->
<service
android:name=".SmsAndPhoneService"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</service>
启动服务
Intent serviceIntent = new Intent(mActivity,SmsAndPhoneService.class);
mActivity.stopService(serviceIntent );
具体代码
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.text.TextUt