Is it possible trap an event from an Android application?

TDowdeswell

Member
Licensed User
Longtime User
If possible I'd like to trap the 'Open text message' event and then pass that message/ message id to my application.

Is this possible and presumably it would mean a Service mosule to sit listening for this event if it was?

If not, I have a Plan B but it is far less elegant so would welcome any pointers.
 

TDowdeswell

Member
Licensed User
Longtime User
Autostart Service crashes

Erel,

Many thanks for your prompt reply, as usual.

I have now tried the approach explained very clearly in another thread of running a service to intercept messages as they come in and then call my app but I have rather 'fallen at the first fence'.

I have added the following Service module, set to auto start at boot, to my Activity as follows:


'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'
Dim MessageFrom1 As String
Dim MessageBody1 As String
Dim SI As SmsInterceptor

End Sub

Sub Service_Create

SI.Initialize("SI")

End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)

MessageFrom1 = From
MessageBody1 = Body
ToastMessageShow(MessageFrom1, True)
'I intend to call my Activity here but it is completely empty as yet

End Sub


When I start the 'phone it displays the error message:

Sorry!
The application TDAutoStart (process.com.TDAutoStart) has stopped unexpectedly.
Please try again.

Force Close (button)

TDAutoStart is the name of my Service module.

The 'phone is a Sony Xperia

Where do I start looking, please? Presumably somethinh is fundamentally wrong!

Thanking you in advance
 
Upvote 0

TDowdeswell

Member
Licensed User
Longtime User
ClassNotFoundException

Thank you both for your replies, I've found out how to get the log now.

Here it is although I'm afraif it isn't helping much. I have seen that other developers are getting the same sort of error on this particular type of 'phone so am now going to try iy on an HTC to see if that's any different.
Unfortunately I only have the Sony on loan until tomorrow morning (GMT) so am rather working against the clock.

LogCat connected to: B4A-Bridge: Sony Ericsson X10i-359419036638384
** Service (tdstart) Create **
** Service (tdstart) Start **
** Service (tdautostart) Create **
** Service (tdautostart) Start **
tdautostart_service_start (B4A line: 12)
StartActivity("tdtext")
java.lang.ClassNotFoundException: com.tdAutoStart.tdtext
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:237)
at java.lang.Class.forName(Class.java:183)
at anywheresoftware.b4a.keywords.Common.getComponentIntent(Common.java:712)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:646)
at com.tdAutoStart.tdautostart._service_start(tdautostart.java:126)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:89)
at com.tdAutoStart.tdautostart.handleStart(tdautostart.java:64)
at com.tdAutoStart.tdautostart.onStart(tdautostart.java:42)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2802)
at android.app.ActivityThread.access$3400(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4263)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.tdAutoStart.tdtext
... 23 more
Caused by: java.lang.ClassNotFoundException: com.tdAutoStart.tdtext in loader

dalvik.system.PathClassLoader@2f792c18
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 23 more
** Activity (main) Create, isFirst = true **
** Service (tdautostart) Create **
** Activity (main) Resume **
** Service (service1) Create **
** Service (service1) Start **
** Service (service1) Start **
Connected to B4A-Bridge

Any help or pointers in the right direction would be great!
 
Upvote 0

TDowdeswell

Member
Licensed User
Longtime User
It cannot find:
B4X:
StartActivity("tdtext")
Are you sure the tdtext Activity is part of the project?

I believe so - hey're both in the same 'container' IE I have a tab with my activity in and a tab with the service. Whan I run it manually it works as expected but:
It won't run at boot on the Xperia
It runs at boot on the HTC but doesn't display anything when I trap an incomin text message.

Is it perhaps a naming issue, there were lots of references to naming in the Android forum I looked at re: this error?

The HTC log for startup is:

LogCat connected to: B4A-Bridge: HTC HTC Wildfire-357758046807162
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It sounds like a long shot but I would be tempted to start the activity using an intent. Maybe that will wake it up.

Something like this.
B4X:
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_MAIN, "")
Intent1.SetComponent("com.google.android.youtube/.HomeActivity")
StartActivity(Intent1)

I still dont understand why it works on one phone and not on another. This is weird.
 
Upvote 0

TDowdeswell

Member
Licensed User
Longtime User
Problem auto-starting Service

Thanks you very much for your help.

I have a rather embarrasing confession to make! I decided to uninstall all the programs I had added to the phone and discovered one that was an old (erroneous) program witha similar name that tried unsuccesfully to load a Service - this was blocking the 'good' program so when I removed it, all sprang into life, after several decades of coding I finally remembered 'back to basics'!

My next problem was when using CallSub to call a sub in my Activity on receipt of a message. This worked fine and I could step through it with the debugger and see that it was loading a layout, filling in fields etc. - great but I could see nothing on the screen!

I overcame this by starting the Activity from the Service with the following:

If IsPaused(Main) Then
StartActivity(Main)
End If

and calling the Sub at the end of StartActivity - works like a dream.

NB I know that apparently it's not good practice to start an Activity from a Service but can't at the moment see another way round it?

Now all I have to do is to somehow make sure the Service doesn't get closed down when the User closes the Activity so it is always running. Maybe StartServiceAt will do this?

Once agin, thanks for the help,
 
Upvote 0

TDowdeswell

Member
Licensed User
Longtime User
StartServiceAt

Good grief, that was quick! I love this function - I just put a call to strat the Service in 15 seconds time on the last line before exiting the Activity and it worked beautifully first time - fantastic!

Excuse my over-enthusiasm but after scratching my head on the previous problem for the last few days this was a very welcome surprise - what a language, great!
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
discovered one that was an old (erroneous) program witha similar name that tried unsuccesfully to load a Service
hmm...i didnt know that could happen! how did you have 2 versions of the same app on your phone?

NB I know that apparently it's not good practice to start an Activity from a Service but can't at the moment see another way round it?
If you do not need to bring the activity to the forefront then you can just make a check on Activity_Resume when the activity can check a status variable from the service to see if anything was done.
[/QUOTE]

Good grief, that was quick! I love this function - I just put a call to strat the Service in 15 seconds time on the last line before exiting the Activity and it worked beautifully first time - fantastic!

Excuse my over-enthusiasm but after scratching my head on the previous problem for the last few days this was a very welcome surprise - what a language, great!
I know! Services are a dream to work with!
Over-enthusiasm is always welcome :)
 
Upvote 0

TDowdeswell

Member
Licensed User
Longtime User
Activity focus

If you do not need to bring the activity to the forefront then you can just make a check on Activity_Resume when the activity can check a status variable from the service to see if anything was done.

That was my dilemma, I did need to make the Activity visible!
 
Upvote 0
Top