结果:
1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧。
2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教)。
3.project -->options…--> users permitions。
3.1 Send SMS
3.2 Read Phone State权限:将其变成 true即可。
如果 read phone state为 false,程序将启动不了,一直黑屏(我没试,原作者是这么说的,你如果没事可以试一试,试过记得给我说一声哦)。
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, 8 FMX.ScrollBox, FMX.Memo, FMX.Layouts, 9 Androidapi.JNI.Net,//需要引入 10 Androidapi.JNI.GraphicsContentViewText,//需要引入 11 Androidapi.JNI.JavaTypes,//需要引入 12 Androidapi.JNIBridge,//需要引入 13 Androidapi.Helpers,//需要引入 14 Androidapi.JNI.Telephony,//需要引入 15 Androidapi.JNI.Os,//需要引入 16 FMX.Platform,//需要引入 17 FMX.Helpers.Android,//需要引入 18 FMX.MediaLibrary.Actions,//需要引入 19 FMX.StdActns,//需要引入 20 FMX.PhoneDialer,//需要引入 21 FMX.Controls.Presentation;//需要引入 22 23 type 24 TForm1 = class(TForm) 25 Label1: TLabel; 26 Memo1: TMemo; 27 Layout1: TLayout; 28 Button1: TButton; 29 Button2: TButton; 30 Button3: TButton; 31 Button4: TButton; 32 Button5: TButton; 33 Button6: TButton; 34 Button7: TButton; 35 procedure Button1Click(Sender: TObject); 36 procedure Button2Click(Sender: TObject); 37 procedure Button3Click(Sender: TObject); 38 procedure Button4Click(Sender: TObject); 39 procedure Button5Click(Sender: TObject); 40 procedure Button6Click(Sender: TObject); 41 procedure Button7Click(Sender: TObject); 42 private 43 { Private declarations } 44 public 45 TelephonyManager: JTelephonyManager; 46 // 打电话、打开地图显示某个坐标点 、发送电子邮件、播放音乐 47 procedure Call_URI(const AAction: JString; const AURI: string); 48 //实现打电话功能 49 procedure PhoneCall(phoneNumber: string); 50 //调用系统程序发短信 51 procedure SentSMSfromIntent(phoneNumber, SMSstring: string); 52 //直接没有任何提示的发送短信 53 procedure SentSMS(phoneNumber, SMSstring: string); 54 //获取Android手机SIM卡串号 55 procedure GetSN; 56 //接收短信 57 function FetchSms: string; 58 { Public declarations } 59 end; 60 61 var 62 Form1: TForm1; 63 64 implementation 65 { $R *.fmx} 66 { $R *.NmXhdpiPh.fmx ANDROID} 67 68 //打电话、打开地图显示某个坐标点 、发送电子邮件、播放音乐 69 procedure TForm1.Call_URI(const AAction: JString; const AURI: string); 70 var 71 uri: Jnet_Uri; 72 Intent: JIntent; 73 begin 74 uri := StrToJURI(AURI); 75 Intent := TJIntent.JavaClass.init(AAction, uri); 76 SharedActivityContext.startActivity(Intent); 77 /// /打开地图显示某个坐标点 78 // Call_URI(TJIntent.JavaClass.ACTION_VIEW, 'geo:38.899533,-77.036476'); 79 end; 80 81 //实现打电话功能 82 procedure TForm1.PhoneCall(phoneNumber: string); 83 var 84 phone: IFMXPhoneDialerService; 85 begin 86 if TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(phone)) then 87 begin 88 phone.Call(phoneNumber); 89 //监听电话状态请用 phone.OnCallStateChanged事件 90 end; 91 end; 92 93 //调用系统程序发短信 94 procedure TForm1.SentSMSfromIntent(phoneNumber, SMSstring: string); 95 var 96 uri: Jnet_Uri; 97 Intent: JIntent; 98 begin 99 uri := StrToJURI('smsto:' + phoneNumber);100 Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_SENDTO, uri);101 Intent.putExtra(StringToJString('sms_body'), StringToJString(SMSstring));102 SharedActivityContext.startActivity(Intent);103 end;104 105 //直接没有任何提示的发送短信106 procedure TForm1.SentSMS(phoneNumber, SMSstring: string);107 var108 j: JSmsManager;109 begin110 j := tjsmsmanager.Create;111 j.sendMultipartTextMessage(StringToJString(phoneNumber), nil, j.divideMessage(StringToJString(SMSstring)), nil, nil);112 end;113 114 //获取Android手机SIM卡串号115 procedure TForm1.GetSN;116 var117 TelephonyServiceNative: JObject;118 begin119 TelephonyServiceNative := SharedActivityContext.getSystemService120 (TJContext.JavaClass.TELEPHONY_SERVICE);121 if Assigned(TelephonyServiceNative) then122 TelephonyManager := TJTelephonyManager.Wrap123 ((TelephonyServiceNative as ILocalObject).GetObjectID);124 // TelephonyManager.getDeviceId 取 IMEI125 // TelephonyManager.getLine1Number 取 MSISDN 手机号,大部分 SIM 卡中不会写入这个信息126 // TelephonyManager.getSimSerialNumber 取 ICCID127 // TelephonyManager.getSubscriberId 取 IMSI 运营商实际上是用这个查询的他那张对应电话号码的表128 end;129 130 //接收短信131 function TForm1.FetchSms: string;132 var133 cursor: JCursor;134 uri: Jnet_Uri;135 address, person, msgdatesent, protocol, msgread, msgstatus, msgtype,136 msgreplypathpresent, Subject, body, servicecenter, locked: string;137 msgunixtimestampms: int64;138 addressidx, personidx, msgdateidx, msgdatesentidx, protocolidx, msgreadidx,139 msgstatusidx, msgtypeidx, msgreplypathpresentidx, subjectidx, bodyidx,140 servicecenteridx, lockedidx: integer;141 begin142 uri := StrToJURI('content://sms/inbox'); //收件箱143 cursor := SharedActivity.getContentResolver.query(//uri, nil, nil, nil, nil);144 // cursor := SharedActivity.145 // managedQuery(146 StrToJURI('content://sms/inbox'), //StrToJURI('content://sms/')所有短信, 含发件箱147 nil,148 StringToJString('1=1) group by (address'),//类似于 SQL 语句,注意,括号只有一半,原因中它已经有一对括号了149 nil,150 StringToJString('date asc')); //desc 降序151 //以上执行的语句是:SELECT * FROM sms WHERE (type=1) AND (1=1) group by (address) order by date asc152 addressidx := cursor.getColumnIndex(StringToJstring('address'));//电话153 personidx := cursor.getColumnIndex(StringToJstring('person'));154 msgdateidx := cursor.getColumnIndex(StringToJstring('date'));155 msgdatesentidx := cursor.getColumnIndex(StringToJstring('date_sent'));156 protocolidx := cursor.getColumnIndex(StringToJstring('protocol'));157 msgreadidx := cursor.getColumnIndex(StringToJstring('read'));158 msgstatusidx := cursor.getColumnIndex(StringToJstring('status'));159 msgtypeidx := cursor.getColumnIndex(StringToJstring('type'));160 msgreplypathpresentidx := cursor.getColumnIndex(StringToJstring('reply_path_present'));161 subjectidx := cursor.getColumnIndex(StringToJstring('subject'));162 bodyidx := cursor.getColumnIndex(StringToJstring('body'));163 servicecenteridx := cursor.getColumnIndex(StringToJstring('service_center'));164 lockedidx := cursor.getColumnIndex(StringToJstring('locked'));165 // while (cursor.moveToNext) do//对所有短信的循环166 // begin167 cursor.moveToLast;//最后一条168 address := JStringToString(cursor.getString(addressidx));169 person := JStringToString(cursor.getString(personidx));170 msgunixtimestampms := cursor.getLong(msgdateidx);171 msgdatesent := JStringToString(cursor.getString(msgdatesentidx));172 protocol := JStringToString(cursor.getString(protocolidx));173 msgread := JStringToString(cursor.getString(msgreadidx));174 msgstatus := JStringToString(cursor.getString(msgstatusidx));175 msgtype := JStringToString(cursor.getString(msgtypeidx));176 msgreplypathpresent := JStringToString(cursor.getString(msgreplypathpresentidx));177 Subject := JStringToString(cursor.getString(subjectidx));178 body := JStringToString(cursor.getString(bodyidx));179 servicecenter := JStringToString(cursor.getString(servicecenteridx));180 locked := JStringToString(cursor.getString(lockedidx));181 Result := IntToStr(trunc(msgunixtimestampms / 1000))+#13182 + '号码:' + address +#13183 + 'person:' + person +#13184 + 'msgdatesent:' + msgdatesent +#13185 + 'protocol:' + protocol +#13186 + 'msgread:' + msgread+#13187 + 'msgstatus:' + msgstatus +#13188 + 'msgtype:' + msgtype +#13189 + 'msgreplypathpresent:' + msgreplypathpresent+#13190 + 'Subject:' + Subject+#13191 + 'servicecenter:' + servicecenter +#13192 + 'locked:' + locked +#13193 + '内容:' + body;194 end;195 196 procedure TForm1.Button1Click(Sender: TObject);197 begin198 Label1.Text := '打电话、发短信和邮件,取得手机 IMEI!';199 end;200 201 procedure TForm1.Button2Click(Sender: TObject);202 begin203 PhoneCall('10086');204 //Call_URI(TJIntent.JavaClass.ACTION_CALL, 'tel:10086');//这个也可以205 end;206 207 procedure TForm1.Button3Click(Sender: TObject);208 begin209 //直接没有任何提示的发送短信,尽量不用,这不道德。短信不限制长度,如超长,自动分条发送210 SentSMS('15810853101', '直接没有任何提示的发送短信');211 end;212 213 procedure TForm1.Button4Click(Sender: TObject);214 begin215 //调用系统程序发短信,短信不限制长度,如超长,自动分条发送216 SentSMSfromIntent('15810853101', '调用系统程序发短信。');217 end;218 219 procedure TForm1.Button5Click(Sender: TObject);220 begin221 //调用系统程序发送电子邮件222 Call_URI(TJIntent.JavaClass.ACTION_SENDTO, 'mailto:513187410@qq.com');223 end;224 225 procedure TForm1.Button6Click(Sender: TObject);226 begin227 GetSN; //先调用一下228 Label1.Text := '本手机 IMEI:' + jstringtostring(TelephonyManager.getDeviceId); //取IMEI;229 end;230 231 procedure TForm1.Button7Click(Sender: TObject);232 begin233 Memo1.Lines.Text := FetchSms;234 end;235 236 end.
这个是测试用的,小伙伴们不要用哦。
1 var 2 cursor: JCursor; 3 uri: Jnet_Uri; 4 address,person,msgdatesent,protocol,msgread,msgstatus,msgtype, 5 msgreplypathpresent,subject,body, 6 servicecenter,locked:string; 7 msgunixtimestampms:int64; 8 addressidx,personidx,msgdateidx,msgdatesentidx,protocolidx,msgreadidx, 9 msgstatusidx,msgtypeidx,msgreplypathpresentidx,subjectidx,bodyidx,10 servicecenteridx,lockedidx:integer;11 begin12 uri:=StrToJURI('content://sms/inbox');13 cursor := SharedActivity.getContentResolver.query(uri, nil, nil,nil,nil);14 addressidx:=cursor.getColumnIndex(StringToJstring('address'));15 personidx:=cursor.getColumnIndex(StringToJstring('person'));16 msgdateidx:=cursor.getColumnIndex(StringToJstring('date'));17 msgdatesentidx:=cursor.getColumnIndex(StringToJstring('date_sent'));18 protocolidx:=cursor.getColumnIndex(StringToJstring('protocol'));19 msgreadidx:=cursor.getColumnIndex(StringToJstring('read'));20 msgstatusidx:=cursor.getColumnIndex(StringToJstring('status'));21 msgtypeidx:=cursor.getColumnIndex(StringToJstring('type'));22 msgreplypathpresentidx:=cursor.getColumnIndex(StringToJstring('reply_path_present'));23 subjectidx:=cursor.getColumnIndex(StringToJstring('subject'));24 bodyidx:=cursor.getColumnIndex(StringToJstring('body'));25 servicecenteridx:=cursor.getColumnIndex(StringToJstring('service_center'));26 lockedidx:=cursor.getColumnIndex(StringToJstring('locked'));27 while (cursor.moveToNext) do begin28 address:=JStringToString(cursor.getString(addressidx));29 person:=JStringToString(cursor.getString(personidx));30 msgunixtimestampms:=cursor.getLong(msgdateidx);31 msgdatesent:=JStringToString(cursor.getString(msgdatesentidx));32 protocol:=JStringToString(cursor.getString(protocolidx));33 msgread:=JStringToString(cursor.getString(msgreadidx));34 msgstatus:=JStringToString(cursor.getString(msgstatusidx));35 msgtype:=JStringToString(cursor.getString(msgtypeidx));36 msgreplypathpresent:=JStringToString(cursor.getString(msgreplypathpresentidx));37 subject:=JStringToString(cursor.getString(subjectidx));38 body:=JStringToString(cursor.getString(bodyidx));39 servicecenter:=JStringToString(cursor.getString(servicecenteridx));40 locked:=JStringToString(cursor.getString(lockedidx));41 Result:=IntToStr(trunc(msgunixtimestampms/1000))+' '+address+' '+body;42 end;43 end;
https://blog.csdn.net/davidtps/article/details/27321801