简体   繁体   English

在Android默认的短信应用中接收和存储短信的正确方法是什么?

[英]What is the right way to receive and store an sms in an android default sms app?

My android app's primary purpose is to delete sms messages easily. 我的Android应用程序的主要目的是轻松删除短信。 Since from kitkat, there is a requirement that the app has to be the "default sms app", I ended up creating an app that has all the necessary receivers. 由于从kitkat开始,该应用程序必须是“默认的短信应用程序”,因此我最终创建了一个具有所有必要接收器的应用程序。

On the SMS Receiver, currently the app is receiving the sms and saving it, with the following code. 在SMS接收器上,当前应用正在接收短信并使用以下代码进行保存。

public override void OnReceive(Context context, Intent intent)
    {
        try
        {
            if (intent.Action != IntentAction) return;
            //context.SendOrderedBroadcast(intent, IntentAction);
            var bundle = intent.Extras;

            if (bundle == null) return;

            var pdus = bundle.Get("pdus");
            var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);

            var msgs = new SmsMessage[castedPdus.Length];

            var sb = new StringBuilder();
            String sender = null;
            for (var i = 0; i < msgs.Length; i++)
            {
                var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
                JNIEnv.CopyArray(castedPdus[i].Handle, bytes);

                msgs[i] = SmsMessage.CreateFromPdu(bytes);
                if (sender == null)
                    sender = msgs[i].OriginatingAddress;
                sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress, System.Environment.NewLine, msgs[i].MessageBody));

                putSmsToDatabase(context, msgs[i]);                    

                Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();

            }
        }
        catch (Exception ex)
        {
            Toast.MakeText(context, ex.Message, ToastLength.Long).Show();
        }
    }
    private void putSmsToDatabase(Context cntxt, SmsMessage sms)
    {
        ContentValues values = new ContentValues();
        values.Put("ADDRESS", sms.OriginatingAddress);
        values.Put("DATE", sms.TimestampMillis);
        values.Put("READ", 0);
        values.Put("STATUS", sms.Status);
        values.Put("TYPE", 1);
        values.Put("SEEN", 0);            
        values.Put("BODY", sms.MessageBody);

        // Push row into the SMS table
        Android.Net.Uri inboxURI = Android.Net.Uri.Parse("content://sms/inbox");
        var uri = cntxt.ContentResolver.Insert(inboxURI, values);            
    }

This code seem to save it, but the problem is that none of the other sms apps shows these messages stored from my app. 这段代码似乎可以保存它,但是问题是,其他任何短信应用都没有显示从我的应用存储的消息。 Everywhere that i searched for SMS receivers, saving part code is not really provided. 我在任何地方搜索SMS接收器,都没有真正提供保存零件代码的功能。 Just the receiving the sms and the Toast display is provided. 仅提供接收短信和Toast显示。 This created the below question. 这产生了以下问题。

  1. As a default sms app, is it really necessary that my app should save the sms to "content://sms/inbox"? 作为默认的短信应用程序,我的应用程序是否真的有必要将短信保存到“ content:// sms / inbox”?
  2. If yes, What is wrong with the save shown in the above sample. 如果是,以上示例中显示的保存有什么问题。 I have a vague understanding that i may have something to do with conversation/threads. 我有一个模糊的理解,我可能与会话/线程有关。 But, since i'm getting started with raw android development, probably i have understood it completely wrong. 但是,由于我是从原始的android开发开始的,所以我可能完全理解它是错误的。 Tried creating the thread using thread_ids, which doesn't seem to work either. 尝试使用thread_ids创建线程,这似乎也不起作用。

This piece of the puzzle is stopping me from the roll out of the app. 这个难题使我无法从应用程序中推出。 Please help. 请帮忙。

All column names should be lowercased as it appears that the Sms ContentResolver is constructing "quoted" column strings within the sql statement. 所有列名都应小写,因为Sms ContentResolver似乎在sql语句中构造“带引号”的列字符串。

While the statements: 虽然声明:

values.Put("ADDRESS", sms.OriginatingAddress);

and

values.Put("address", sms.OriginatingAddress);

Should resolve to a SQL statement that is case insensitive, it is not. 应该解析为不区分大小写的SQL语句,不是。

Thus you should provide the columns names as returned from Telephony.TextBasedSmsColumns (which are all lowercase): 因此,您应该提供从Telephony.TextBasedSmsColumns返回的列名(均为小写):

Your code becomes: 您的代码变为:

ContentValues values = new ContentValues();
values.Put("address", sms.OriginatingAddress);
~~~

If your app is targeting a min. 如果您的应用的目标是分钟。 of API19: API19:

ContentValues values = new ContentValues();
values.Put(Telephony.TextBasedSmsColumns.Address, sms.OriginatingAddress);
~~~

The SMS ContentProvider was not publicly documented until API 19(?): 直到API 19(?)才公开记录SMS ContentProvider:

Telephony.TextBasedSmsColumns : TextBasedSmsColumns Telephony.TextBasedSmsColumnsTextBasedSmsColumns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM