接收短信部分,将短信猫设置成系统短信后即可,使用过小米、三星验证ok,附带github代码
1.Androidmanifest.xml
<!-- Activity that allows the user to send new SMS/MMS messages --> <activity android:name=".activity.ComposeSmsActivity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </activity> <!-- Services --> <service android:name="com.yjf.smsmall.service.SmsReceiverService"/> <!-- BroadcastReceiver that listens for incoming MMS messages --> <receiver android:name="com.yjf.smsmall.receiver.MmsReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH"> <intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> <data android:mimeType="application/vnd.wap.mms-message" /> </intent-filter> </receiver> <!-- Receiver for pre kitkat. This is disabled when it's kitkat and above --> <receiver android:name="com.yjf.smsmall.receiver.SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:enabled="true" > <intent-filter > <action android:name="android.provider.Telephony.SMS_RECEIVED" /> <action android:name="android.provider.Telephony.SMS_DELIVER"/> </intent-filter> </receiver>
<!--enabled必须设置为true,否则收不到短信-->
<service android:name="com.yjf.smsmall.service.HeadlessSmsSendService" android:exported="true" android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"> <intent-filter> <action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="sms"/> <data android:scheme="smsto"/> <data android:scheme="mms"/> <data android:scheme="mmsto"/> </intent-filter> </service>
public class ComposeSmsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } }
public class HeadlessSmsSendService extends Service { @Override public IBinder onBind(Intent intent) { return null; } }
public class MmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { } }
public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { intent.setClass(context, SmsReceiverService.class); intent.putExtra("result", getResultCode()); SmsReceiverService.beginStartingService(context, intent); } }MainActivity调用一下短信读取
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Uri uriSms = Uri.parse("content://sms/inbox"); uriSms = uriSms.buildUpon().appendQueryParameter("LIMIT", "10").build(); String[] projection = { "_id", "address", "date", "body" }; Cursor c = getContentResolver().query(uriSms, projection, null, null, "date DESC"); } }
SmsReceiverService 服务里获取并处理接收到的短信
github:https://github.com/yujfei/SmsMall.git