gpt4 book ai didi

java - Android 以编程方式在内容提供程序中保存或插入彩信

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:54 25 4
gpt4 key购买 nike

我正在尝试下面描述的代码,我从互联网上获得它。它在内容提供程序(mmssms.db)中正确插入行,但 MMS 未显示 native 消息应用程序。

thread_id 11 已存在于我的内容提供程序中,因此我使用 thread_id 11。

public static Uri insert(Context context, String to, String subject, byte[] imageBytes)
{
try
{
Uri destUri = Uri.parse("content://mms");

// Get thread id
//Set<String> recipients = new HashSet<String>();
//recipients.addAll(Arrays.asList(to));
long thread_id = getOrCreateThreadId(context, to);
Log.e(">>>>>>>", "Thread ID is " + thread_id);

// Create a dummy sms
ContentValues dummyValues = new ContentValues();
dummyValues.put("thread_id", thread_id);
dummyValues.put("body", "Dummy SMS body.");
Uri dummySms = context.getContentResolver().insert(Uri.parse("content://sms/"), dummyValues);

// Create a new message entry
long now = System.currentTimeMillis();
ContentValues mmsValues = new ContentValues();
mmsValues.put("thread_id", thread_id);
mmsValues.put("date", now/1000L);
mmsValues.put("msg_box", Mms.MESSAGE_BOX_OUTBOX);
//mmsValues.put("m_id", System.currentTimeMillis());
mmsValues.put("read", 1);
mmsValues.put("sub", subject);
mmsValues.put("sub_cs", 106);
mmsValues.put("ct_t", "application/vnd.wap.multipart.related");
mmsValues.put("exp", imageBytes.length);
mmsValues.put("m_cls", "personal");
mmsValues.put("m_type", 128); // 132 (RETRIEVE CONF) 130 (NOTIF IND) 128 (SEND REQ)
mmsValues.put("v", 19);
mmsValues.put("pri", 129);
mmsValues.put("tr_id", "T"+ Long.toHexString(now));
mmsValues.put("resp_st", 128);

// Insert message
Uri res = context.getContentResolver().insert(destUri, mmsValues);
String messageId = res.getLastPathSegment().trim();
Log.e(">>>>>>>", "Message saved as " + res);

// Create part
createPart(context, messageId, imageBytes);


createAddr(context, messageId, to);


//res = Uri.parse(destUri + "/" + messageId);

// Delete dummy sms
context.getContentResolver().delete(dummySms, null, null);

return res;
}
catch (Exception e)
{
e.printStackTrace();
}

return null;
}

private static Uri createPart(Context context, String id, byte[] imageBytes) throws Exception
{
ContentValues mmsPartValue = new ContentValues();
mmsPartValue.put("mid", id);
mmsPartValue.put("ct", "image/png");
mmsPartValue.put("cid", "<" + System.currentTimeMillis() + ">");
Uri partUri = Uri.parse("content://mms/" + id + "/part");
Uri res = context.getContentResolver().insert(partUri, mmsPartValue);
Log.e(">>>>>>>", "Part uri is " + res.toString());

// Add data to part
OutputStream os = context.getContentResolver().openOutputStream(res);
ByteArrayInputStream is = new ByteArrayInputStream(imageBytes);
byte[] buffer = new byte[256];
for (int len=0; (len=is.read(buffer)) != -1;)
{
os.write(buffer, 0, len);
}
os.close();
is.close();

return res;
}

private static Uri createAddr(Context context, String id, String addr) throws Exception
{
ContentValues addrValues = new ContentValues();
addrValues.put("address", addr);
addrValues.put("charset", "106");
addrValues.put("type", 151); // TO
Uri addrUri = Uri.parse("content://mms/"+ id +"/addr");
Uri res = context.getContentResolver().insert(addrUri, addrValues);
Log.e(">>>>>>>", "Addr uri is " + res.toString());

return res;
}
@SuppressLint("NewApi")
private static long getOrCreateThreadId(Context context, String recipients) throws Exception
{

return 11;
}

最佳答案

在 Android 4.4 上,只有默认消息应用程序可以写入短信​​和彩信内容解析器,因此您可能在 Android 4.4 或更早版本的设备上尝试过 http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

关于java - Android 以编程方式在内容提供程序中保存或插入彩信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com