gpt4 book ai didi

android - Kitkat 及更高版本的传入消息接收器和通知默认系统消息应用程序

转载 作者:太空狗 更新时间:2023-10-29 13:20:20 26 4
gpt4 key购买 nike

大家好,我正在开发一个消息应用程序,并按照谷歌博客 link here 的所有说明进行操作

它工作得很好。但是当我的应用程序作为默认消息应用程序工作时我遇到问题我想将消息保存在我的应用程序数据库以及设备默认消息应用程序数据库中,以便当设备消息应用程序变为默认值时所有消息都可以也可以在那里看到。

重点是当我将我的应用程序用作默认应用程序时,我如何通知默认消息传递应用程序保存消息。

请帮忙!抱歉英语不好

#更新 1
为了保存在默认数据库中,我使用以下代码

public class SmsReceiver extends BroadcastReceiver {

private Context context;
private String msg_from;
//private MessageDataBaseAdapter messageDataBaseAdapter;
private String msgBody;
public static final String SMS_EXTRA_NAME = "pdus";
public static final String SMS_URI = "content://sms";

public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final String SEEN = "seen";

public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;

public static final int MESSAGE_IS_NOT_READ = 0;
public static final int MESSAGE_IS_READ = 1;

public static final int MESSAGE_IS_NOT_SEEN = 0;
public static final int MESSAGE_IS_SEEN = 1;

@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
if (intent.getAction().equals("android.provider.Telephony.SMS_DELIVER")) {
HideSMSToast.showShortToast("Your Message Hasbeen received");

Bundle bundle = intent.getExtras(); // ---get the SMS message passed
// Get ContentResolver object for pushing encrypted SMS to incoming folder
ContentResolver contentResolver = context.getContentResolver(); // in---
SmsMessage[] msgs = null;

if (bundle != null) {
// ---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
// String strMessageFrom =
// bundle.getDisplayOriginatingAddress();
for (int i = 0; i < msgs.length; i++) {
//Calendar c = Calendar.getInstance();
// System.out.println("Current time => "+c.getTime());
// SCSLToast.showShortToast(c.getTime().toString());
//SimpleDateFormat date = new SimpleDateFormat(
// "dd-MMM-yyyy hh:mm:ss a");
//String formattedDate = date.format(c.getTime());

msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
//Db operation
saveSmsDataToDefaulDB( contentResolver, msgs[i] );
}
///
} catch (Exception e) {
// Log.d("Exception caught",e.getMessage());
//messageDataBaseAdapter.close();
}
}

}
}

private void saveSmsDataToDefaulDB( ContentResolver contentResolver, SmsMessage sms )
{
// Create SMS row
ContentValues values = new ContentValues();
values.put( ADDRESS, sms.getOriginatingAddress());
values.put( DATE, sms.getTimestampMillis());
values.put( READ, MESSAGE_IS_NOT_READ);
values.put( STATUS, sms.getStatus());
values.put( TYPE, MESSAGE_TYPE_INBOX);
values.put( SEEN, MESSAGE_IS_NOT_SEEN);
contentResolver.insert( Uri.parse(SMS_URI), values);
HideSMSToast.showShortToast("Content written in default message app db");
}}

我在默认系统messagenig应用程序中收到了消息,但地址我得到了正确的,没有其他数据..
请告诉我如何在默认消息传递数据库中存储数据

这是当我将我的应用程序设为默认应用程序时我如何在系统默认短信应用程序中收到消息的屏幕截图
enter image description here

# 更新 2
谢谢大家的支持我已经解决了这是我犯的愚蠢错误。我正在更新我的代码如下
private void saveSmsDataToDefaulDB( ContentResolver contentResolver, SmsMessage sms )
{
// Create SMS row
ContentValues values = new ContentValues();
values.put( ADDRESS, sms.getOriginatingAddress());
values.put( DATE, sms.getTimestampMillis());
values.put( READ, MESSAGE_IS_NOT_READ);
values.put( STATUS, sms.getStatus());
values.put( TYPE, MESSAGE_TYPE_INBOX);
values.put( SEEN, MESSAGE_IS_NOT_SEEN);
values.put(BODY, sms.getMessageBody());
contentResolver.insert( Uri.parse(SMS_URI), values);
// HideSMSToast.showShortToast("Content written in default message app db");
}

最佳答案

我认为它应该工作,创建一个新类

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}

}

我的意思是这里的代码是在 toast 中打印的。但是一旦你收到消息并且发件人没有,只需将它发送到你的数据库。

希望能帮助到你...

关于android - Kitkat 及更高版本的传入消息接收器和通知默认系统消息应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29840507/

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