Reading SMS inbox

1,852 views
Skip to first unread message

Mauritz Zondagh

unread,
Jul 22, 2016, 5:18:21 AM7/22/16
to DroidScript
Hi
Is there a way to read the mobile sms inbox? The sample provided with droidsctipt receives incomming sms (broadcast receiver), but i would like to scan through existing messages in the inbox?
A example would be much appreciated if possible.

Thanks

Alex F

unread,
Jul 22, 2016, 2:14:02 PM7/22/16
to DroidScript
I think you can only get a callback when you received an sms

sms= app.CreateSMS();
sms.OnMessage(receive)

//I don't know exactly how it works. Try it out
function receive(msg) {
alert(msg)
//alert(msg).GetText();
}

Mauritz Zondagh

unread,
Aug 10, 2016, 6:47:00 AM8/10/16
to DroidScript
Hi,

Yes the Broadcast receive will receive a sms as it arrives. However, i would like to scan the existing sms inbox.
Went through Google, cannot find a solution in javascript, many examples in java.

1. Is there a way to include java code into my droidscript project?
2. Is there a way to handle content providers in javascript?
3. Is there a free or paid plugin for this sms inbox reader?
4. Can i use the PhoneGap sms plugin in Droidscript?
5. Are there any plans to provide such Android functionality plugins for Droidscrip in the near future if it does not exist?
6. Is there a website where plugins are located?

Then a suggestion for these java guru's writing plugins. Why not write a sms_inbox plugin, contacts_plugin, caller_ plugin e.g. call history and make it a paid plugin. I would not bundle it all together - the developers can make more money by selling the functionality with individual plugins per functionality.

Thanks



Steve Garman

unread,
Aug 10, 2016, 7:57:11 AM8/10/16
to DroidScript

This is something that would require a Java plugin to be created for DroidScript.

If you are interested in doing this yourself, I would recommend using the AIDE app on android to create the plugin.

The premium version of DroidScript comes with an sdk entry in the main menu to get started but if you do not have a Premium subscription, you can still access it.

You can get that menu as a non-premium user by creating a file called _sdk_ (with no extension) in your DroidScript folder on your phone or tablet.

If you can find a cordova/phonegap plugin that does what you want, it would be a good start for building a DS plugin.

If this is not something you want to tackle yourself,it may be worth asking a Java developer to turn it into a DS plugin.

Or you might consider sponsoring the DS team to create a DS plugin from the phonegap plugin you have found to share with the community.

If you wish to ask a private Java programmer to develop the plugin and she does not wish to use AIDE, the sdk is also available for Eclipse. http://androidscript.org/sdk/

Mauritz Zondagh

unread,
Aug 10, 2016, 8:43:43 AM8/10/16
to DroidScript
Hi Steve, thank you for your answer.

I am not sure now, creating a java plugin might be a difficult task for me. I installed the SDK menu in Droidscipt to create a SDK with AEDI (__sdk__). Will see if i can get some notes on how to create a plugin (SDK?) Will see if i can get a JAVA example compiled and running in AEDI???, and then what needs to be done further to make it a SDK!! 

I was actually trying to get away from JAVA when i started javascript with Droidscript.
I had similar problem in QT for Android - no sms inbox support, and i could not launch a non static java procedure from QT C++. Now i am stuck at the same problem LOL!!

I do not have the Phonegap plugin, it was just  mentioned many times on google, and i was wondering if one can import another manufactures plugin into Droidscript. Does not seem possible.
Also is does not seem possible to add JAVA directly to my javascript code - need a plugin.

I  know someone with javascript experience - will ask if they have some ideas how to create plugin ext. If i get something working i will post in in this group, but it is a big IF!!


Dave Smart

unread,
Aug 11, 2016, 7:56:02 AM8/11/16
to DroidScript
If you find a Java programmer, then I have a plugin sample which wraps a cordova plugin.  It contains various support files which make it easier to port cordova plugins to DroidScript.

Nick Weber

unread,
Aug 11, 2016, 10:54:12 AM8/11/16
to DroidScript
I'm a java programmer :)

Mauritz Zondagh

unread,
Aug 11, 2016, 2:52:46 PM8/11/16
to DroidScript
Hi, I started to write a JAVA plugin for Droidscript, for reading the sms inbox, later adding managing sms inbox ext. I followed the droidscript spk from Chris https://groups.google.com/d/msg/androidscript/NgsEdMST6HE/y-miUeVdAwAJ to create a AIDE project for a plugin. Excellent script from Chris and very easy to follow.

Parameter passing :
In this example plugin, the GetVersion procedure call in Java returns a single string to Droidscript. Can anyone please help me with a example (or modifying the example from Chris files(.jave & .inc) how to pass parameters to your java procedure from Droidscript e.g.string,int and multiple parameters? Is it possible to return string, int and multiple return values?

SMS Java:
To test my sms inbox reading java procedures i started a new project in AIDE (when done i will copy to plugin project). I am not a JAVA programmer, rather C++ but i manage to expand the hello world project to call 2 procedures, and return the int answer, and print to the Log view. I then wanted to insert the JAVA code to read a sms from the inbox. I got the java code from google http://pulse7.net/android/read-sms-message-inbox-sent-draft-android/ and inserted into my MainActivity.java.

I get a error "This method can not be referenced from a static context" at line ResultString = Read_SMS_Inbox(1)
If anyone can help me to get this sms procedure working from a static contect???? i will appreciate it.. There is some other errors on "adaptor" in the sms procedure - still need to sort this out.
(Strangely when i was working in QT C++ with JNI static call to JAVA to read the SMS i got the same  error - was not able to solve it.)

package com.mycompany.myapp;

import android.app.*;
import android.os.*;

// ***************************
// MZH imports
// ***************************

//import My_Java_Procedures.*;

import android.content.*;
import android.util.Log;
import android.graphics.*;
import java.io.*;
import java.lang.reflect.*;

// For sms
//import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
//import android.os.Bundle;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
//import android.widget.TextView;

// ***************************
public class MainActivity extends Activity {
    
    //public static final String TAG = "Basic Network Demo"; // MZH
    //private static boolean wifiConnected = false; // MZH

// Global variables
//private static int Result; // It is important to have the static keywork in front
//private static String ResultString;

@Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
Log.d("MyApp","Start my App - Launch go");
go(1); // MZH
    }

// ***************************
// My Code start here
// ***************************


public static void go(int k) {
// Local Variables
int Result;
String ResultString;

Result = My_Java_Procedure_Call(7);
ResultString = "" + Result;
Log.d("MyApp",ResultString);
ResultString = Read_SMS_Inbox(1);
Log.d("MyApp",ResultString);
}


public int My_Java_Procedure_Call(int n) {
Log.d("MyApp","Procedure Call successful");
return n*2;
    }

// Permissions in AndroidMenifest.xml file <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

public static String Read_SMS_Inbox(int MessageNumber) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
 
// List required columns
String[] reqCols = new String[] { "_id", "address", "body" };
 
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
 
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);


adapter = new SimpleCursorAdapter(this, R.layout.row, c,
                    new String[] { "body", "address" }, new int[] {
                            R.id.lblMsg, R.id.lblNumber });
return adaptor;
// ***************************
}

}



Thanks



Dave Smart

unread,
Aug 12, 2016, 6:56:49 AM8/12/16
to DroidScript
Hi Mauritz,

Passing parameters from JavaScript in your .inc file is easy, you just list them and DS will convert them to int, float, string for you. (take a look at the .inc files on the github examples).

If you want to return multiple parameters from a single method call, then you will need to encode your data into a string.  The best way to do this is probably to use JSON.  You can then use JSON.parse() in the .inc file and split out the parameters.  Alternatively you could just put them in a pipe '|' separated list and use the JavaScript split function to pull them out, or perhaps create a javascript object in a string and just use the JavaScript eval function to convert the string into an object

eg. just return  "{ name:'fred', width:34, height:33 }" from your plugin and then use the eval() function around your method call in the .inc file.

Security Note: if the data from you plugin can come from external sources, then using eval() is not secure and the JSON approach is safer.

Regards
David

Mauritz Zondagh

unread,
Aug 12, 2016, 11:27:48 AM8/12/16
to DroidScript
Still busy with my sms plugin. thank you for the tip on parsing parameters in/out of plugin - i will try it.

Regarding my sample java code provided above. I manage to get the static/non static procedure call and reading the content database into string thus not using simplecursoradaptor. sorted out. Will post some code later for other users when i sort it out completely.

However my app crash when i query the database. On reading Google i found a article about new requirements for user permissions from Marshmallow OS. Google changed the permission system so that the user give permission on runtime, and therefore you need additional coding!! + normal permission in your manifest file.  If the user did not give that permission and you access the content provider query, then your app crash. Exactly why my app is doing now.

So i inserted the code as per "https://androidhub.intel.com/en/posts/blundell/You_have_permission_to.html"  but i get errors on the import of some libraries in AIDE. AIDE highlights in red the "support" part.

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

Does anyone have a idea how i can fix this?

Thanks

Dave Smart

unread,
Aug 13, 2016, 7:12:05 AM8/13/16
to androi...@googlegroups.com
The easiest way to get around this problem is probably by targeting a lower version of Android.

DroidScript targets version 21 for the build process

Also, note that DS already includes android-support-v4.jar and you might get library clashing issues at runtime if you try and use it in a plugin.

Mauritz Zondagh

unread,
Aug 13, 2016, 11:12:49 AM8/13/16
to DroidScript
I managed to read my SMS inbox from a AIDE project. The 2 main issues was

1. library : I had to create a libs dir (same level as src), and copy 2 libraries called android-support-v4.jar & android-support-v7-appcompat.jar.
These files i downloaded with Android app SDK manager (Playstore)

2.I think AIDE targets Marshmallow (the os installed on my cellphone), and my app crashed when querying the contents database (SMS inbox). To solve this, i had to comply to the new Marshmallow permission methods, and in the manifest file i had to include 
<uses-permission-sdk-23/> together with the normal SMS permissions.

Dave Smart posted
"DroidScript targets version 21 for the build process & DS already includes android-support-v4.jar and you might get library clashing issues at runtime if you try and use it in a plugin.". 
This i do not know how to solve yet.
All i have is a AIDE java app which works, but uses API23 & the 2 libraries. Not sure how to target API 21 in AIDE and ....?

For the benefit of other users, i attached my AIDE MainActivity.java & Manifest file for reading all the sms from inbox to string, and display on log. (Not sure how to display on the canvas yet, and how to take out the Hello world LOL - Will get there)

I also attached a Plugin.java file, which uses Chris AIDE project template. I kept the main command GetVersion for now but linked the result to my sms string, for testing. However i get errors of Unknown method 'getContentResolver,& ...extCompat.checkPermissions..... can not be applied to com.mycompany.plugins.user.t3plugin.java.lang.String ext)
I suspect something to do the Dave's post? Need to check.
I also did not check out the official AIDE plugin generator yet.

Thanks for all the suggestions posted. It all helps to get to the result.
If anyone can help to port my Java AIDE to a plugin format, it would be appreciate.









AndroidManifest.xml
MainActivity.java
T3Plugin.java

Mauritz Zondagh

unread,
Aug 16, 2016, 4:02:26 PM8/16/16
to DroidScript
Hi Dave,

You mentioned that you have plugin sample which wraps a cordova plugin.
On Github, there is a content provider cordova plugin https://github.com/phearme/cordova-ContentProviderPlugin
(Not sure if this is what is needed?)

Do you need a Java programmer to combine these two?
Is there a user, or someone from the Droidscript developers group who have the knowledge and could  help to do this?
It would be a good paid plugin for Droidscript, but maybe not so good idea if the main code is from a free source / cordova.

I am still trying to get my working content provided Java app ported to the plugin template to compile. I got stuck with 6 errors -All related i think to the same problem!

Dave Smart

unread,
Aug 17, 2016, 5:50:09 AM8/17/16
to DroidScript
Actually Mauritz ,

DroidScript already allows access to the content provider.  You might find that you can already do what you want to do in DS.  Check out the Calendar access demo in the latest alpha.

If that works, then you could just create a pure JavaScript plugin :)

(It's possible I might need to add some permissions to DS for it to work)

Regards
David

Mauritz Zondagh

unread,
Aug 17, 2016, 7:24:40 AM8/17/16
to DroidScript
Hi Dave,
Thank you for this tip, i think it will do the job. Calendar access is a content provider service.
After many hours of struggling with this problem, i am actually still a newbie. Where do i find the Droidscript Alpha release?

thanks

Alex F

unread,
Aug 17, 2016, 8:47:58 AM8/17/16
to DroidScript
In google playstore or
unofficial versions:
http://androidscript.org/alpha/

Mauritz Zondagh

unread,
Aug 17, 2016, 9:18:15 AM8/17/16
to DroidScript
Thanks found it and downloaded latest version DroidScript_136a4.apk 09-Aug-2016 15:22 5.4M
 But i do not find the Calendar access demo  in the samples. Is this maybe a premium sample?

Steve Garman

unread,
Aug 17, 2016, 11:27:26 AM8/17/16
to DroidScript
This is what I am using for calendar access in the latest alpha http://pastebin.com/KbEuizAk

Beware! This does not work in the current release of DroidScript.

Mauritz Zondagh

unread,
Aug 17, 2016, 1:45:25 PM8/17/16
to DroidScript
Thank you, the calendar sample is working 100% in the alpha release of Droidscrip 1.36a4.
I modified is slightly to access the sms inbox, and reads a empty string from the content provider e.g. []
I guess it is because no permissions have been set. In Androidstudio is is done in manifest file, where do i set permissions in Droidscript? 
"Dave's comment It's possible I might need to add some permissions to DS for it to work" - Does this mean permissions is something which Droidscript app need to have build in,
or is it something i should insert somewhere?

Thanks


Steve Garman

unread,
Aug 17, 2016, 1:50:45 PM8/17/16
to DroidScript
If it is a permissions issue, Dave will need to add it to the DroidScript manifest.

Dave Smart

unread,
Aug 18, 2016, 7:04:23 PM8/18/16
to DroidScript
Let me know what permissions you need added to DS and I will add them to the next alpha release :)
Message has been deleted

Mauritz Zondagh

unread,
Aug 19, 2016, 8:44:07 AM8/19/16
to DroidScript
Hi Dave, i need to read and delete SMS in the SMS inbox (data base) via content providers, so i guess the permission are

<uses-permission android:name="android.permission.READ_SMS" />
       Not sure what permissions i need to delete a entry in the database for SMS?

        these 2 probably not needed for only reading a sms from database
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

With the new Alpha release allowing content providers access, should you not add all the permissions for SMS inbox, Contacts and Calender read,delete, add/send access?

I want to delete a entry from the SMS inbox data base (content provider) - does anyone know how to do this in javascript?

Thanks

Steve Garman

unread,
Aug 19, 2016, 10:31:48 AM8/19/16
to DroidScript
Dave,

I suppose it would be worth considering the following missing permissions

WRITE_CONTACTS
READ_SMS
WRITE_SMS

Alex F

unread,
Aug 19, 2016, 11:03:59 AM8/19/16
to DroidScript
It would be nice if we could get all possible intents an app supports :)

Emma Nevada

unread,
Jun 23, 2017, 9:23:58 AM6/23/17
to DroidScript
what's the progress on this?? :D

Mauritz Zondagh

unread,
Jun 23, 2017, 9:53:45 AM6/23/17
to DroidScript
I unfortunately could not get this working, used Broadcast receiver instead.

However, in Droidscript Premium there is a example for content providers. Not sure what the example does, but probably you will need content providers for reading the sms inbox / Phone contact ext.




Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages