如何在android中将短信保存到收件箱?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/642076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save SMS to inbox in android?
提问by Josef Pfleger
I have written the below code for sending SMS messages.
我编写了以下用于发送短信的代码。
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(destAddr, null, mMessageText, il, null);
But this is not updating in my Inbox, I need to save the same message in Inbox, Or is there any way to invoke a native SMS application to send SMS ?
但这不是在我的收件箱中更新,我需要在收件箱中保存相同的消息,或者有什么方法可以调用本机短信应用程序来发送短信?
回答by Josef Pfleger
You can use the sms content provider to read and write sms messages:
您可以使用 sms content provider 来读取和写入 sms 消息:
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
I don't know why you would want to write a message you sendto the inboxbut if that is what you want just change the above uri to "content://sms/inbox"
.
我不知道你为什么要写一条你发送到收件箱的消息,但如果这是你想要的,只需将上面的 uri 更改为"content://sms/inbox"
.
Alternatively you can hand over to a messaging application by starting an activity with an intent similar to the following:
或者,您可以通过启动具有类似于以下意图的活动来移交给消息传递应用程序:
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://"));
sendIntent.putExtra("address", "123456789");
sendIntent.putExtra("sms_body", "foo bar");
startActivity(sendIntent);
Edit:However, the sms://
content provider is not part of the SDK so I strongly recommend notusing code like this in public applications for several reasons.
编辑:但是,sms://
内容提供程序不是 SDK 的一部分,因此出于多种原因,我强烈建议不要在公共应用程序中使用这样的代码。
回答by Pir Fahim Shah
If you want to manually put some SMS to your inbox with a sender name then,
如果您想手动将一些带有发件人姓名的短信发送到您的收件箱,那么,
ContentValues values = new ContentValues();
values.put("address", "+923359110795");//sender name
values.put("body", "this is my text");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
also add these in manifest.
还将这些添加到清单中。
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Now this code will add sms to inbox with a defined sender name, so you can easily handle you problem with this solution,
现在此代码将使用定义的发件人名称将短信添加到收件箱,因此您可以轻松地使用此解决方案处理问题,
回答by Atif Mahmood
This code will work for all Android versions including above kitkat (19)
此代码适用于所有 Android 版本,包括上述 kitkat (19)
public boolean saveSms(String phoneNumber, String message, String readState, String time, String folderName) {
boolean ret = false;
try {
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
values.put("read", readState); //"0" for have not read sms and "1" for have read sms
values.put("date", time);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Uri uri = Telephony.Sms.Sent.CONTENT_URI;
if(folderName.equals("inbox")){
uri = Telephony.Sms.Inbox.CONTENT_URI;
}
mActivity.getContentResolver().insert(uri, values);
}
else {
mActivity.getContentResolver().insert(Uri.parse("content://sms/" + folderName), values);
}
ret = true;
} catch (Exception ex) {
ex.printStackTrace();
ret = false;
}
return ret;
}
How to call
如何打电话
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final String myPackageName = getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
startActivityForResult(intent, 1);
}else {
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
}else {
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
onActivityResult
活动结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final String myPackageName = getPackageName();
if (Telephony.Sms.getDefaultSmsPackage(mActivity).equals(myPackageName)) {
//Write to the default sms app
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
}
}
}
}
For more detail or sample app follow link: http://wisdomitsol.com/blog/android/sms/how-to-programmatically-save-sms-to-inbox-or-sent-in-android
有关更多详细信息或示例应用程序,请点击链接:http: //wisdomitsol.com/blog/android/sms/how-to-programmatically-save-sms-to-inbox-or-sent-in-android
回答by Fonan Kone
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", multimessage);
values.put("read", 1); //"0" for have not read sms and "1" for have read sms
Uri uri = Telephony.Sms.Sent.CONTENT_URI;
Activity ctx = this.cordova.getActivity();
ctx.getContentResolver().insert(uri, values);
回答by zhou533
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
invoke a native SMS application with content
使用内容调用本机 SMS 应用程序