Android build apk is so slow on android phones....

39 views
Skip to first unread message

kyr...@gmail.com

unread,
Jul 10, 2015, 4:51:09 AM7/10/15
to codenameone...@googlegroups.com
I made an app for android and ios. I just finished it and I sent in an android build just to test it on my phone. When I run the app in device simulator it words perfectly. However when I downloaded it onto my Samsung Galaxy S3 it became so slow, like practically unresponsive, switching between forms took minutes and swiping between tabs takes 30 seconds. I also tried it on a Samsung Galaxy S4 and the same thing happened. Is codename one's performance on android really that bad because then what's the point of even using cn1. I must be doing something wrong. Got any idea?

David Wafula

unread,
Jul 10, 2015, 5:20:24 AM7/10/15
to CodenameOne Discussions
CN1 is fast ...(it compiles into native remember). There could be any number  of things 'wrong' with your app. If you provide a test case, then that can be helpful for diagnosis. I use Samsung Galaxy s3 and s4 and my apps run perfectly fine.

On Fri, Jul 10, 2015 at 10:51 AM, <kyr...@gmail.com> wrote:
I made an app for android and ios. I just finished it and I sent in an android build just to test it on my phone. When I run the app in device simulator it words perfectly. However when I downloaded it onto my Samsung Galaxy S3 it became so slow, like practically unresponsive, switching between forms took minutes and swiping between tabs takes 30 seconds. I also tried it on a Samsung Galaxy S4 and the same thing happened. Is codename one's performance on android really that bad because then what's the point of even using cn1. I must be doing something wrong. Got any idea?

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/b9550ca2-1b60-4761-9787-f349f6977f58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
David Wafula

Kyri Ioulianou

unread,
Jul 10, 2015, 5:22:30 AM7/10/15
to codenameone...@googlegroups.com

Alright. How do I provide this 'test case' ?

You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/1p9J0uNiIa8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.

David Wafula

unread,
Jul 10, 2015, 5:32:49 AM7/10/15
to CodenameOne Discussions
Sample code of your app ?


For more options, visit https://groups.google.com/d/optout.



--
David Wafula

Kyri Ioulianou

unread,
Jul 10, 2015, 7:19:53 AM7/10/15
to codenameone...@googlegroups.com
Here you go
package com.mycompany.myapp;

import com.codename1.components.InfiniteProgress;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.JSONParser;
import com.codename1.io.NetworkManager;
import com.codename1.io.Preferences;
import com.codename1.location.Location;
import com.codename1.location.LocationManager;
import com.codename1.ui.*;
import com.codename1.ui.List;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.GridLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.util.UIBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
* Created by Kyri on 2015/06/25.
*/
public class MyApplication {

private NetworkManager manager = NetworkManager.getInstance();
private Form current;
private Tabs tabs;
private Form main;
private Form makeStatusForm;
private UIBuilder u;
private final String URL = "http://********";
private final String URL2 = "http://localhost/Webservice/";
private int currentTabId = 0;
private ArrayList<MessagePost> posts = new ArrayList<MessagePost>();

public void init(Object context) {
try {
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
} catch (IOException e) {
e.printStackTrace();
}
}

public void start() {
if (current != null) {
current.show();
return;
}
u = new UIBuilder();
showMainForm();
//Check whether or not this is the first time the user is using the app
checkFirstTime();
//Updates the array list of posts from the server
updatePosts();

}
//Container for each tab.
private Container singleTabCont(String message, String distance, String time) {

//Container tabContainer = new Container(new BorderLayout());

BorderLayout bl = new BorderLayout();
bl.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);
Container tabContainer = new Container(bl);
tabContainer.setUIID("ContainerDefault");

Container containerTop = new Container(new BorderLayout());
tabContainer.addComponent(BorderLayout.NORTH, containerTop);

Container containerBlack = new Container();
containerBlack.setUIID("Container1");

Container containerAbove = new Container(new BorderLayout());
Label labelDistance = new Label(distance);
labelDistance.setUIID("Label2");
Label labelTime = new Label(time);
labelTime.setUIID("Label2");
containerAbove.addComponent(BorderLayout.EAST, labelDistance);
containerAbove.addComponent(BorderLayout.WEST, labelTime);

containerTop.addComponent(BorderLayout.NORTH, containerAbove);
containerTop.addComponent(BorderLayout.CENTER, containerBlack);

Container cn2 = new Container();
cn2.setUIID("Container1");
tabContainer.addComponent(BorderLayout.SOUTH, cn2);

TextArea ta = new TextArea();
ta.setFocusable(false);
ta.setEditable(false);
ta.setText(message);
ta.setUIID("Label1");
tabContainer.addComponent(BorderLayout.CENTER, ta);

return tabContainer;
}

public void stop() {
current = Display.getInstance().getCurrent();
}

public void destroy() {
}

private ActionListener makeStatus = new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {

main.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 200));
showMakeStatusForm();

}

}
;

public void checkFirstTime() {
Preferences.
set("FirstTime", false);
if (!Preferences.get("FirstTime", false)) {
tabs.addTab("", singleTabCont("Welcome to ____, Scroll left for the next post -->","",""));
tabs.addTab("", singleTabCont("All messages are completely anonymous and are sorted according to location","",""));
tabs.addTab("", singleTabCont("The closest messages to you you will see first","",""));
Preferences.set("FirstTime", true);
}
}

public Image getImageFromRes(String imageName) {
try {
Resources resFile = Resources.
openLayered("/theme");
Image image = resFile.getImage(imageName);
return image;
} catch (IOException ex) {
}
return null;
}
//The main from where the tabs are
public void showMainForm() {

main = new Form("");
main.setLayout(new GridLayout(3, 1));
Container cn1 = new Container();
main.setScrollableY(false);

tabs = new Tabs();
tabs.hideTabs();
tabs.setUIID("Tabs1");

Container cn2 = new Container(new BorderLayout());

main.addComponent(cn1);
main.addComponent(tabs);
main.addComponent(cn2);

main.setUIID("Form1");

Container containerBottom = new Container(new BorderLayout());
cn2.addComponent(BorderLayout.SOUTH, containerBottom);

Button makePostButton = new Button("");
makePostButton.setUIID("ButtonMakePost");
makePostButton.setIcon(getImageFromRes("makepostunpressed.png"));
makePostButton.setPressedIcon(getImageFromRes("makepostpressed.png"));
makePostButton.addActionListener(makeStatus);

containerBottom.addComponent(BorderLayout.EAST, makePostButton);

main.show();


}
//The second form
public void showMakeStatusForm() {

final Form form = (Form) u.createContainer("/theme", "GUI 2");
form.show();
final Button doneButton = (Button) u.findByName("Button1", form);
Button cancelButton = (Button) u.findByName("Button", form);
final TextArea ta = (TextArea) u.findByName("TextArea", form);

doneButton.addActionListener(new ActionListener() {

double longitude, latitude;

@Override
public void actionPerformed(ActionEvent evt) {

ta.setHint("Uploading...");
doneButton.setEnabled(false);

LocationManager locationManager = LocationManager.getLocationManager();
try {

Location location = locationManager.getCurrentLocation()
;
longitude = location.getLongitude();
latitude = location.getLatitude();

} catch (IOException e) {
e.printStackTrace()
;
}

if(ta.getText().length() < 2) {
ta.setText("");
ta.setHint("Message post too short");
} else {

ConnectionRequest cr =
new ConnectionRequest() {
@Override
protected void handleException(Exception err) {
super.handleException(err);
}
}
;

System.out.println("longitude " + longitude + " latitude " + latitude);

cr.setDuplicateSupported(true);
cr.setPost(true);
cr.addArgument("message", ta.getText());
cr.addArgument("longitude", longitude + "");
cr.addArgument("latitude", latitude + "");
cr.setUrl(URL + "postMessage.php");
cr.setFailSilently(false);
manager.addToQueueAndWait(cr);

form.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 200));

showMainForm();
updatePosts();
}
}

})
;

cancelButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent evt) {
form.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 200));
main.show();
}
})
;
}
//Updating the array list of posts and filling it into the tabs
public void updatePosts() {

double longitude=0;
double latitude = 0;

posts.clear();

LocationManager locationManager = LocationManager.getLocationManager();
try {

Location location = locationManager.getCurrentLocation()
;
longitude = location.getLongitude();
latitude = location.getLatitude();

} catch (IOException e) {
e.printStackTrace()
;
}

ConnectionRequest cr =
new ConnectionRequest() {
@Override
protected void handleException(Exception err) {
super.handleException(err);
}
}
;
cr.setDuplicateSupported(true);
cr.setPost(true);
cr.addArgument("longitude", longitude + "");
cr.addArgument("latitude", latitude + "");
cr.setUrl(URL + "fetchPosts.php");
cr.setFailSilently(true);
manager.addToQueueAndWait(cr);

byte[] data = cr.getResponseData();
Map tempMap = (parseJsonObject(data));
java.util.List listMessages = (java.util.List) tempMap.get("posts");

if(listMessages!=null && !listMessages.isEmpty()) {
for(Object listMessage : listMessages) {
Map tempListMessage = (Map) listMessage
;
MessagePost messagePost = new MessagePost(tempListMessage.get("messagePost").toString().trim(), Integer.parseInt(tempListMessage.get("distance").toString().trim()), Integer.parseInt(tempListMessage.get("timeS").toString().trim()));
posts.add(messagePost);
tabs.addTab("", singleTabCont(messagePost.getMessage(), messagePost.calculateDistance(), messagePost.calcTime()));

} main.revalidate();

}

}

public Map parseJsonObject(byte[] data) {
try {
JSONParser parser =
new JSONParser();
Map response = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
return response;
} catch (IOException ioe) {
}
return null;
}
//I don't use this method as it only removed every second tab for some reason
public void clearTabs() {
System.out.println(tabs.getTabCount()+
"");
        for(int i = 0; i<tabs.getTabCount(); i++) {
tabs.removeTabAt(i);
} main.revalidate();
System.out.println(tabs.getTabCount()+"");

}

}

Shai Almog

unread,
Jul 10, 2015, 9:55:11 AM7/10/15
to codenameone...@googlegroups.com, kyr...@gmail.com, kyr...@gmail.com
There's a bit much to read there but I suggest you narrow down the lines that are slow.

I noticed that you use addToQueueAndWait which will wait until a network operation completes, do you do this before navigating?
Reply all
Reply to author
Forward
0 new messages