歡迎來到天天文庫
瀏覽記錄
ID:38699937
大小:43.00 KB
頁數:5頁
時間:2019-06-17
《手機發(fā)送短信ANDRIOD代碼》由會員上傳分享,免費在線閱讀,更多相關內容在教育資源-天天文庫。
1、首先建立兩個類:一個是Activitiy一個是BroadcastReiver第一activtiy類代碼1.importandroid.app.Activity;2.importandroid.content.IntentFilter;3.4.5.publicclassQTMessageextendsActivity{6.?????7.??//調用BroadcastReceiver8.??private??SMSBroadcastReceiverReceuver=null;9.???10.??//和xml那個一樣11.??privatestaticfinalStringSMS_
2、ACTION="android.provider.Telephony.SMS_RECEIVED";12.??@Override13.??publicvoidonCreate(BundlesavedInstanceState){14.??????super.onCreate(savedInstanceState);15.??????setContentView(R.layout.main);16.??????//調用BroadcastReceiver17.??????Receuver=newSMSBroadcastReceiver();18.??????//和該3、filter>匹配19.????????IntentFilterfintent=newIntentFilter();20.????????fintent.addAction(SMS_ACTION);21.????????QTMessage.this.registerReceiver(Receuver,fintent);22.??????23.??}24.}復制代碼第二類BroadcastReiver1.importjava.text.SimpleDateFormat;2.importjava.util.Date;3.1.importandroid.content.Broadc4、astReceiver;2.importandroid.content.Context;3.importandroid.content.Intent;4.importandroid.telephony.SmsManager;5.importandroid.telephony.SmsMessage;6.importandroid.widget.Toast;7.8.publicclassSMSBroadcastReceiverextendsBroadcastReceiver{9.10.11.????@Override12.????publicvoidonReceive(Conte5、xtcontext,Intentintent){13.????????System.out.println("ok");14.????????Object[]pdus=(Object[])intent.getExtras().get("pdus");//獲取短信內容15.16.????????for(Objectpdu:pdus){17.18.????????????byte[]data=(byte[])pdu;//獲取單條短信內容,短信內容以pdu格式存在19.20.????????????SmsMessagemessage=SmsMessage.createFromPdu6、(data);//使用pdu格式的短信數據生成短信對象21.22.????????????Stringcontent=message.getMessageBody();//獲取短信的內容23.24.????????????Datedate=newDate(message.getTimestampMillis());//獲取短信的時間25.26.????????????SimpleDateFormatformat=newSimpleDateFormat(27.????????????????????"yyyy-MM-ddHH:mm:ss");28.29.????????????7、Stringsendtime=format.format(date);30.????????????//顯示短信信息31.????????????Toast.makeText(context,content,Toast.LENGTH_LONG).show();1.?????????????2.????????}3.????}4.5.}復制代碼分享一下本人中其中一個應用(手機短信轉發(fā))的代碼,主要是實現短信的發(fā)送功能。如果想做一些短信相關的應用,以下的代碼可能對你有幫助。1.packagerbase.ap
3、filter>匹配19.????????IntentFilterfintent=newIntentFilter();20.????????fintent.addAction(SMS_ACTION);21.????????QTMessage.this.registerReceiver(Receuver,fintent);22.??????23.??}24.}復制代碼第二類BroadcastReiver1.importjava.text.SimpleDateFormat;2.importjava.util.Date;3.1.importandroid.content.Broadc
4、astReceiver;2.importandroid.content.Context;3.importandroid.content.Intent;4.importandroid.telephony.SmsManager;5.importandroid.telephony.SmsMessage;6.importandroid.widget.Toast;7.8.publicclassSMSBroadcastReceiverextendsBroadcastReceiver{9.10.11.????@Override12.????publicvoidonReceive(Conte
5、xtcontext,Intentintent){13.????????System.out.println("ok");14.????????Object[]pdus=(Object[])intent.getExtras().get("pdus");//獲取短信內容15.16.????????for(Objectpdu:pdus){17.18.????????????byte[]data=(byte[])pdu;//獲取單條短信內容,短信內容以pdu格式存在19.20.????????????SmsMessagemessage=SmsMessage.createFromPdu
6、(data);//使用pdu格式的短信數據生成短信對象21.22.????????????Stringcontent=message.getMessageBody();//獲取短信的內容23.24.????????????Datedate=newDate(message.getTimestampMillis());//獲取短信的時間25.26.????????????SimpleDateFormatformat=newSimpleDateFormat(27.????????????????????"yyyy-MM-ddHH:mm:ss");28.29.????????????
7、Stringsendtime=format.format(date);30.????????????//顯示短信信息31.????????????Toast.makeText(context,content,Toast.LENGTH_LONG).show();1.?????????????2.????????}3.????}4.5.}復制代碼分享一下本人中其中一個應用(手機短信轉發(fā))的代碼,主要是實現短信的發(fā)送功能。如果想做一些短信相關的應用,以下的代碼可能對你有幫助。1.packagerbase.ap
此文檔下載收益歸作者所有