使用Delphi XE8拦截android中的传入短信

时间:2016-12-13 14:38:15

标签: android delphi sms telephony

我制作了一个Delphi XE8应用,试图在短信到达终端时收到通知,我不愿意使用 TTimer 定期扫描收件箱。我使用了BroadCast Reciever,但收到短信时,我的应用程序崩溃了文字:"不幸的是,应用程序MYAPP1已关闭"。帮助!

PS:我的SDK排在第23位 清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" android:label="%activityLabel%" android:configChanges="orientation|keyboard|keyboardHidden" android:launchMode="singleTask"> <meta-data android:name="android.app.lib_name" android:value="%libNameValue%" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.embarcadero.notifications.FMXNotificationAlarm" /> <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> </manifest>

type
  JTelephonySmsIntentsClass = interface(IJavaClass)      
    ['{73FFA042-DAE3-497F-B9D4-B7F1B178E941}']      
    function getMessagesFromIntent(...);      
    ...     
  end;      
  
  [JavaSignature('android/provider/Telephony/Sms/Intents')]      
  JTelephonySmsIntents = interface (IJavaInstance)              ['{42798E28-0E3A-4287-8EE9-606BB9D311B5}']      
  end;  

  TJTelephonySmsIntents = class(TJavaGenericImport<JTelephonySmsIntentsClass, JTelephonySmsIntents>) end;  

procedure BCRecvr(Context: JContext; Intent: JIntent);
var
  I: Integer;
  msgs: TJavaArray<JSmsMessage>;
  msg : JSmsMessage;
  newPhoneNumber, text : string;
begin
  if Intent.getActn.cmpareTo(TJTelephSmsIntents.JavaClass.SMS_RECEIVED_ACTION) = 0 then
  begin
    msgs := TJTelephSmsIntents.JavaClass.getMessFromIntent(Intent);
    for I := 0 to msgs.Length - 1 do
    begin
      msg := msgs[i];
      newPhoneNumber := JStringToString(msg.getOriginatingAddress);
      text := JStringToString(msg.getDisplayMessageBody);
      Memo1.Lines.Add(newPhoneNumber + ': ' + text);
    end;
  end;
end;

0 个答案:

没有答案