码迷,mamicode.com
首页 > 移动开发 > 详细

Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

时间:2015-08-11 21:09:28      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

结语:

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 var
108   j: JSmsManager;
109 begin
110   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 var
117   TelephonyServiceNative: JObject;
118 begin
119   TelephonyServiceNative := SharedActivityContext.getSystemService
120   (TJContext.JavaClass.TELEPHONY_SERVICE);
121   if Assigned(TelephonyServiceNative) then
122   TelephonyManager := TJTelephonyManager.Wrap
123   ((TelephonyServiceNative as ILocalObject).GetObjectID);
124   // TelephonyManager.getDeviceId 取 IMEI
125   // TelephonyManager.getLine1Number 取 MSISDN 手机号,大部分 SIM 卡中不会写入这个信息
126   // TelephonyManager.getSimSerialNumber 取 ICCID
127   // TelephonyManager.getSubscriberId 取 IMSI 运营商实际上是用这个查询的他那张对应电话号码的表
128 end;
129 
130 //接收短信
131 function TForm1.FetchSms: string;
132 var
133   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 begin
142   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 asc
152   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   // begin
167   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))+#13
182   + 号码: + address +#13
183   + person: + person +#13
184   + msgdatesent: + msgdatesent +#13
185   + protocol: + protocol +#13
186   + msgread: + msgread+#13
187   + msgstatus: + msgstatus +#13
188   + msgtype: + msgtype +#13
189   + msgreplypathpresent: + msgreplypathpresent+#13
190   + Subject: + Subject+#13
191   + servicecenter: + servicecenter +#13
192   + locked: + locked +#13
193   + 内容: + body;
194 end;
195 
196 procedure TForm1.Button1Click(Sender: TObject);
197 begin
198   Label1.Text := 打电话、发短信和邮件,取得手机 IMEI!;
199 end;
200 
201 procedure TForm1.Button2Click(Sender: TObject);
202 begin
203   PhoneCall(10086);
204   //Call_URI(TJIntent.JavaClass.ACTION_CALL, ‘tel:10086‘);//这个也可以
205 end;
206 
207 procedure TForm1.Button3Click(Sender: TObject);
208 begin
209   //直接没有任何提示的发送短信,尽量不用,这不道德。短信不限制长度,如超长,自动分条发送
210   SentSMS(15810853101, 直接没有任何提示的发送短信);
211 end;
212 
213 procedure TForm1.Button4Click(Sender: TObject);
214 begin
215   //调用系统程序发短信,短信不限制长度,如超长,自动分条发送
216   SentSMSfromIntent(15810853101, 调用系统程序发短信。);
217 end;
218 
219 procedure TForm1.Button5Click(Sender: TObject);
220 begin
221   //调用系统程序发送电子邮件
222   Call_URI(TJIntent.JavaClass.ACTION_SENDTO, mailto:513187410@qq.com);
223 end;
224 
225 procedure TForm1.Button6Click(Sender: TObject);
226 begin
227   GetSN; //先调用一下
228   Label1.Text := 本手机 IMEI: + jstringtostring(TelephonyManager.getDeviceId); //取IMEI;
229 end;
230 
231 procedure TForm1.Button7Click(Sender: TObject);
232 begin
233   Memo1.Lines.Text := FetchSms;
234 end;
235 
236 end.

 

Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

标签:

原文地址:http://www.cnblogs.com/FKdelphi/p/4722097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!