Android短信查询功能—利用BroadcastReceiver实现短信查询
程序员文章站
2022-03-03 21:49:49
利用BroadcastReceiver实现短信查询...
MainActivity如下:
package cc.testsmsbroadcastreceiver;
import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 利用BroadcastReceiver实现短信查询
*
* 注意权限:
* <uses-permission android:name="android.permission.RECEIVE_SMS"/>
*
* 详细资料:
* http://blog.csdn.net/lfdfhl/article/details/8195400
*
*/
public class MainActivity extends Activity{
private TextView mTextView;
private SMSBroadcastReceiver mSMSBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mTextView=(TextView) findViewById(R.id.textView);
mSMSBroadcastReceiver=new SMSBroadcastReceiver();
mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() {
@Override
public void OnReceived(String message) {
mTextView.setText(message);
}
});
}
}
SMSBroadcastReceiver如下:
package cc.testsmsbroadcastreceiver;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
/**
* 配置广播接收者:
* <receiver android:name=".SMSBroadcastReceiver">
* <intent-filter android:priority="1000">
* <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
* </intent-filter>
* </receiver>
*
* 注意:
* <intent-filter android:priority="1000">表示:
* 设置此广播接收者的级别为最高
*/
public class SMSBroadcastReceiver extends BroadcastReceiver {
private static MessageListener mMessageListener;
public SMSBroadcastReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
Object [] pdus= (Object[]) intent.getExtras().get("pdus");
for(Object pdu:pdus){
SmsMessage smsMessage=SmsMessage.createFromPdu((byte [])pdu);
String sender=smsMessage.getDisplayOriginatingAddress();
String content=smsMessage.getMessageBody();
long date=smsMessage.getTimestampMillis();
Date timeDate=new Date(date);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=simpleDateFormat.format(timeDate);
System.out.println("短信来自:"+sender);
System.out.println("短信内容:"+content);
System.out.println("短信时间:"+time);
mMessageListener.OnReceived(content);
//如果短信来自5556,不再往下传递
if("5556".equals(sender)){
System.out.println(" abort ");
abortBroadcast();
}
}
}
// 回调接口
public interface MessageListener {
public void OnReceived(String message);
}
public void setOnReceivedMessageListener(MessageListener messageListener) {
this.mMessageListener=messageListener;
}
}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true" />
</RelativeLayout>
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cc.testsmsbroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cc.testsmsbroadcastreceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
本文地址:https://blog.csdn.net/lfdfhl/article/details/25914611
下一篇: 媳妇不能惯的太厉害了