MainActivity
public class MainActivity extends Activity {
Button btn_open;
Button btn_close;
private HelloBr hello_br = null;
//Hello_br是繼承BroadcastReceiver的java檔
private static final String SMS_Action="android.provider.Telephony.SMS_RECEIVED";
//攔截簡訊的行為
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_open = (Button) this.findViewById(R.id.button1);
btn_close = (Button) this.findViewById(R.id.button2);
btn_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("連結監聽器");
hello_br=new HelloBr();
// 建立一個BroiadcastReceiver 物件
IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction(SMS_Action);
//為intentFilter加入一個行為
MainActivity.this.registerReceiver(hello_br, intentFilter);
//將BroadcastReceiver物件註冊到系統當中
}
});
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("解除監聽器");
//解除BroadcastReceiver物件的註冊
MainActivity.this.unregisterReceiver(hello_br);
}
});
}
}
HelloBr
public class HelloBr extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("接收到訊息");
// !取得SMS------------------------------------------------
Bundle bundle = intent.getExtras();
Object[] myObj = (Object[]) bundle.get("pdus");
// 在Bundle 物件當中有一個屬性名為pdus(SMS的key),這個屬性的值是一個Object 陣列
SmsMessage smsMsg[] = new SmsMessage[myObj.length];
// 建立一個SmsMessage 型別的陣列
System.out.println(myObj.length);
String msg = "";
for (int i = 0; i < myObj.length; i++) {
smsMsg[i] = SmsMessage.createFromPdu((byte[]) myObj[i]);
// 使用Object 陣列當中的物件建立SmsMessage 物件
msg += "發文者:" + smsMsg[i].getOriginatingAddress() + "\n";
// 取得接收傳送的號碼
msg += "簡訊內容:" + smsMsg[i].getDisplayMessageBody() + "\n";
// 取得接受簡訊內容
}
// !------------------------------------------------
Intent it = new Intent(context, SMS.class);
it.putExtra("SMSData", msg);
// 把值傳到SMS
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 加上重開機後,會自動開啟Activity,並在AndroidManifiest加上action,使用靜態註冊
context.startActivity(it);
}
}
SMS-->接受攔截到的簡訊顯示用
public class SMS extends Activity{
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_sms);
tv=(TextView) this.findViewById(R.id.textView1);
String sms=this.getIntent().getStringExtra("SMSData");
tv.setText(sms);
}
}
AndroidManifste
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_brsms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hello_brsms.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="HelloBr">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name="SMS"></activity>
</application>
</manifest>
加上*重新啟動的Activity的Action,也要加上uses-permission(因為這個action是靜態註冊所以要加在AndroidManifset裡)
一定要註冊receiver
留言列表