Android Tutorial Android home screen widgets tutorial - part I

Massimiliano

Member
Licensed User
Hello,
I have a widget with a timer showing a clock and a PhoneStateListener showing some cell data on events raise (onSignalStrengthsChanged and onCellLocationChanged).
After a while (1 hour about of phone sleeping), it seems that the timer event and/or PhoneStateListener events are not raised anymore (the widget labels doesn't refresh)

Any hint?
I'm using b4a v5.02

Thank you
 

Massimiliano

Member
Licensed User
@lemonisdead
No, i'm not using phonewakestate, do you think this is the trick?

I thought that the power of the screen the sleeping services would return active. Isn't it?

Probaly I'm loosing something

I'll give it a try
Thank you
 

Massimiliano

Member
Licensed User
I'll try Erel... Thank you ;-)
Erel it seems that adding
#StartCommandReturnValue: android.app.Service.START_STICKY
after a while it's raised an error at line

Sub Service_Start(StartingIntent As Intent)
--->>>> If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Any hint?
Thank you
b4a v5.02
 

Massimiliano

Member
Licensed User
There is an error at wgtservice.java un line 201, object should be initialized first

B4X:
public static String  _service_start(anywheresoftware.b4a.objects.IntentWrapper _startingintent) throws Exception{
//BA.debugLineNum = 22;BA.debugLine="Sub Service_Start(StartingIntent As Intent)";
//BA.debugLineNum = 24;BA.debugLine="PSL_SVC.startListening";
_psl_svc.startListening();
//BA.debugLineNum = 26;BA.debugLine="If rv.HandleWidgetEvents(StartingIntent) Then Ret";
if (_rv.HandleWidgetEvents(processBA,(android.content.Intent)(_startingintent.getObject()))) { //<---- THIS IS LINE 201
if (true) return "";};
//BA.debugLineNum = 27;BA.debugLine="End Sub";
return "";
}
removing #StartCommandReturnValue: android.app.Service.START_STICKY the problem doesn't appear anymore
 

Erel

Administrator
Staff member
Licensed User
I see. Change the code to:
B4X:
If StartingIntent.IsInitialized AND rv.HandleWidgetEvents(...)
In sticky mode an empty intent is sent when the process is recreated.
 

Massimiliano

Member
Licensed User
I see. Change the code to:
B4X:
If StartingIntent.IsInitialized AND rv.HandleWidgetEvents(...)
In sticky mode an empty intent is sent when the process is recreated.
Hi Erel this was perfect....

But I have another question :)

My app together with the widget sticky service has other activities. You can launch these activities tapping the widget logo or launching the application in the standard way.
When android starts the widget takes only 3 mb RAM.
if i start an activity and then i close it, memory consumption grows and never decreases when all activities are closed (i'm pretty sure
Activity.Finish is called)

any suggestions?

Thanks
Massimiliano
 

Erel

Administrator
Staff member
Licensed User
Are you using B4A v5? There was a memory leak in v4 related to layout animations.
 

Massimiliano

Member
Licensed User
Are you using B4A v5? There was a memory leak in v4 related to layout animations.
Yes, i'm using v5.02
My android is Lollipop 5.1 (released by Motorola 3 days ago in italy via OTA) on my moto-g-lte-xt1039, but also with Kitkat 4.4.4 i've found this problem, so i'm pretty sure it's not related to an android problem.
The memory consumption (widget only) is 3.2mb after a phone restart.
After launching some activities inside the apk package the memory consumption grows (sometime it goes over 100mb).
for sure i'm using a lot of libraries, but i expect that on Activity.finish all should be released from memory

I tried ExitApplication, but it's like i was Killing myself with my witget service! :)
 
Last edited:

Erel

Administrator
Staff member
Licensed User
The memory usage is allowed to grow. The question is whether you eventually get an out of memory exception?
 

corwin42

Expert
Licensed User
After launching some activities inside the apk package the memory consumption grows (sometime it goes over 100mb).
for sure i'm using a lot of libraries, but i expect that on Activity.finish all should be released from memory
If you call Activity.finish all resources bound to this activity are marked as unused and in the next GC cycle they will be freed. The application process itself will keep running and if you store data in process global variables this memory will still be used. If you use many bitmaps they probably may stay in memory, too for some time.
So without knowing your application and code we can't say if it is ok or not. But this whole memory stuff is not related to this tutorial so if you have further questions please start a new thread on this in the questions forum.
 

Massimiliano

Member
Licensed User
The memory usage is allowed to grow. The question is whether you eventually get an out of memory exception?
No memory exception, but lot of mem consumption compared to other apps.

I know this thread isn't the right place. I will open the right one, if I will be able to give more information.

Many thanks
 

imgsimonebiliato

Well-Known Member
Licensed User
Hello,
I tried to implent into the event "ImageView_click" this code

B4X:
Sub iv1_Click
Dim i as ImageView
i = Sender
End Sub
But the app crash.
Here it is the log:

B4X:
java.lang.ClassCastException: anywheresoftware.b4a.objects.RemoteViewsWrapper cannot be cast to android.widget.ImageView
I can't do it?
 

Erel

Administrator
Staff member
Licensed User
You cannot directly access the widget views.
Sender will be RemoteView not ImageView.
 
Top