Android使用BroadCastRecevier廣播實現接收簡訊,並利用Toast彈出顯示內容
阿新 • • 發佈:2019-01-01
在上一篇文章 Android簡單實現BroadCastReceiver廣播機制 中簡單的實現了一個廣播機制,這裡利用BroadCarstRecevier實現一個接收簡訊並顯示內容的案例,當然至於接收到簡訊以後是顯示還是做其它的工作都可以由你自己決定了,我現在只關心的是獲取到簡訊內容就可以了。
看例項:
MainActivity.java 採用預設,不需要任何修改
MyBroadcastReceiver.java
AndroidManifest.xml
這裡關於收發簡訊的許可權
package com.example.broadcastreceiver; import android.os.Bundle; import android.view.Menu; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
package com.example.broadcastreceiver; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import android.widget.Toast; public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Object[] pduses = (Object[]) intent.getExtras().get("pdus");// 得到所有的簡訊 for (Object pdus : pduses) { byte[] pdusmessage = (byte[]) pdus;// 得到每一條簡訊 SmsMessage sms = SmsMessage.createFromPdu(pdusmessage);// 建立簡訊bean String mobile = sms.getOriginatingAddress();// 得到傳送方的電話號碼 String content = sms.getMessageBody();// 得到簡訊內容 Date date = new Date(sms.getTimestampMillis());// 得到傳送簡訊的具體時間 // 2009-10-12 12:21:23 SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", Locale.US);// 為時間設定格式 String sendtime = format.format(date); Toast.makeText(context, mobile + content + sendtime, Toast.LENGTH_LONG).show(); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastreceiver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.SEND_SMS" /> <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="com.example.broadcastreceiver.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=".MyBroadcastReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" > </action> </intent-filter> </receiver> </application> </manifest>
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />不能忘記