我希望能帮助你在android kitkat上找到一个可以阅读短信的代码。我知道,该应用程序必须是具有相关权限的默认短信应用程序。我真的不想用任何要求用户手动选择默认sms应用程序的弹出对话框来烦扰用户。我希望我的应用程序在其任务完成之前作为默认sms执行,然后在使用我的应用程序之前将设置恢复为用户或工厂设置的原始默认sms。
目前,我不能显示任何短信,也看不到为什么。然而,我看到我的应用程序列在测试设备上的默认sms应用程序中。
下面是我使用的代码:
function TForm1.Process_SMS: string;
var
cursor: JCursor;
uri: Jnet_Uri;
address, person, msgdatesent, protocol, msgread, msgstatus, msgtype,
msgreplypathpresent, subject, body,
smsid, servicecenter, locked: string;
msgunixtimestampms: int64;
id_smsid, addressidx, personidx, msgdateidx, msgdatesentidx, protocolidx,
msgreadidx, msgstatusidx, msgtypeidx, msgreplypathpresentidx, subjectidx,
bodyidx, servicecenteridx, lockedidx: integer;
begin
uri := StrToJURI('content://sms/inbox');
cursor := SharedActivity.getContentResolver.query(uri, nil, nil, nil, nil);
id_smsid := cursor.getColumnIndex(StringToJstring('_id'));
addressidx := cursor.getColumnIndex(StringToJstring('address'));
personidx := cursor.getColumnIndex(StringToJstring('person'));
msgdateidx := cursor.getColumnIndex(StringToJstring('date'));
msgdatesentidx := cursor.getColumnIndex(StringToJstring('date_sent'));
protocolidx := cursor.getColumnIndex(StringToJstring('protocol'));
msgreadidx := cursor.getColumnIndex(StringToJstring('read'));
msgstatusidx := cursor.getColumnIndex(StringToJstring('status'));
msgtypeidx := cursor.getColumnIndex(StringToJstring('type'));
msgreplypathpresentidx := cursor.getColumnIndex(StringToJstring('reply_path_present'));
subjectidx := cursor.getColumnIndex(StringToJstring('subject'));
bodyidx := cursor.getColumnIndex(StringToJstring('body'));
servicecenteridx := cursor.getColumnIndex(StringToJstring('service_center'));
lockedidx := cursor.getColumnIndex(StringToJstring('locked'));
while (cursor.moveToNext) do
begin
smsid := JStringToString(cursor.getString(id_smsid));
address := JStringToString(cursor.getString(addressidx));
person := JStringToString(cursor.getString(personidx));
msgunixtimestampms := cursor.getLong(msgdateidx);
msgdatesent := JStringToString(cursor.getString(msgdatesentidx));
protocol := JStringToString(cursor.getString(protocolidx));
msgread := JStringToString(cursor.getString(msgreadidx));
msgstatus := JStringToString(cursor.getString(msgstatusidx));
msgtype := JStringToString(cursor.getString(msgtypeidx));
msgreplypathpresent := JStringToString(cursor.getString(msgreplypathpresentidx));
subject := JStringToString(cursor.getString(subjectidx));
body := JStringToString(cursor.getString(bodyidx));
servicecenter := JStringToString(cursor.getString(servicecenteridx));
locked := JStringToString(cursor.getString(lockedidx));
Listbox1.Items.Add(subject);
// I plan on deleting messages here
//SharedActivity.getContentResolver.delete(uri, StringToJString('_ID=' + smsid), nil);
Result := IntToStr(trunc(msgunixtimestampms/1000)) + ' ' + address + ' ' + body;
end;
end;我是这么说的:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
label1.text := Process_SMS;
end;下面是我使用的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.embarcadero.ezt"
android:versionCode="19"
android:versionName="1.0.0"
android:installLocation="preferExternal">
<!-- This is the platform API where NativeActivity was introduced. -->
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<uses-permission android:name="android.permission.BROADCAST_WAP_PUSH" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-feature android:glEsVersion="0x00020000" android:required="True"/>
<application android:persistent="False"
android:restoreAnyVersion="False"
android:label="ezt"
android:debuggable="True"
android:largeHeap="False"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<!-- Our activity is a subclass of the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
android:label="ezt"
android:configChanges="orientation|keyboard|keyboardHidden"
android:launchMode="singleTask">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="ezt" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" />
<!-- BroadcastReceiver that listens for incoming SMS messages -->
<receiver android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
</manifest>
<!-- END_INCLUDE(manifest) -->在我的测试设备上,我选择了我的应用程序作为默认的短信应用程序,在个人->安全->应用程序权限下,我将其设置为“始终允许”我的应用程序读取短信。我的应用程序的界面只有一个列表框和两个快速按钮。
请并感谢您的帮助。我迷路了..。
发布于 2016-01-01 15:21:03
今天,我用另一部手机向我的测试设备发送了两条短信,之前我也收到过两条短信。这意味着总共有4条消息在那里(测试设备)。当我现在运行我的应用程序时,我看到了最后两条短信。我不知道为什么,我只看到了最后两个。我很高兴我看到了一些东西,因为现在可以继续开发了。我现在已经禁用了"app权限“。我也没有将我的应用程序设置为默认短信。奇怪的是,它甚至在这种配置下也能工作。无论如何,感谢每一个查看这个问题的人。
https://stackoverflow.com/questions/34520148
复制相似问题