Tien
Tien

Reputation: 71

Why is this app running slow?

Why does this app stop answering then I press play? It sometimes show a "the application is not responding" but it works if I wait.

It works nice on my emulator, but not on my phone (or any other phone I tried).

All it does is streaming sound.

package comunicera.se;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ListActivity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Godnattsaga extends ListActivity {
/** Called when the activity is first created. */


ListView list;
TextView spelandesSagqa;
//private ProgressDialog mProgressDialog;
 ProgressBar myProgressBar;
 int myProgress = 0;
 MediaPlayer mp = new MediaPlayer();
 String BASEURL = "http://godnattsaga.nu/sagor";
 public static long glCurrentSaga = 0;
 public static String giCode = null;
 int giAntalSagor = 0;
 int possWhenPaused = 0;
 ProgressBar myBufferProgressBar;
 TextView buffrarText;
 int progress;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    {

        new Thread(buffer).start();
        String lsSagor = getFileFromUrl(BASEURL+"/gratisSagor.txt");

        final String[] laList = lsSagor.split("~");

        giAntalSagor = laList.length;

        //String saga = laList[0].replaceAll("#", "\n");
        String[] laSaga = laList[0].split("#");

        final String[] guiLaList = new String[giAntalSagor];
        for (int i = 0; i < giAntalSagor; i++)
        {
            guiLaList[i] = laList[i].replaceAll("#", "\n");
        }

        changeSpelandesSaga(laSaga[0]);

        setList (guiLaList);


        ListView list = getListView();
        list.setTextFilterEnabled(true);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                        String liCode = kop(id);

                        glCurrentSaga = id;
                        String[] laSaga = laList[(int) glCurrentSaga].split("#");
                        changeSpelandesSaga(laSaga[0]);
                    }
        });

        final Button button = (Button) findViewById(R.id.SpelaPause);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                selectDownloadOrPLay(laList);
            }
        });
        glCurrentSaga = 0;
        changeSpelandesSaga(laSaga[0]);



    }
    catch (Exception e) 
    {
        changeSpelandesSaga("Check your connection, are you in flightmode?");

    }
}

public void selectDownloadOrPLay(String[] laList) {
    String[] laSaga = laList[(int) glCurrentSaga].split("#");



    String url =  BASEURL+"/gratis/"+laSaga[0].replaceAll(" ", "_")+".mp3";

    if (mp.isPlaying())
    {
        mp.pause();
        possWhenPaused=mp.getCurrentPosition();
    }
    else if (possWhenPaused != 0)
    {
        mp.start();
    }
    else
    {       
        startSaga (url);
    }

}

private String kop(long id) 
{
    mp.pause();

    return "gratis";
}
public void setList (String[] laList)
{
    /*
     *
      final String[] lAList = new String[3];

    lAList[0] = "Saga 1 \n";
    lAList[1] = "Saga 2 \n";        
    lAList[2] = "Saga 3";

    setList (lAList);

     * 
     */


    setContentView(R.layout.main);
    ArrayAdapter<String> appointmentList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, laList);
    list=(ListView)findViewById(android.R.id.list);
    list.setAdapter(appointmentList);
}
public void changeSpelandesSaga(String sagaRubrik)
{
    possWhenPaused = 0;

    TextView t = new TextView(this); 
    t=(TextView)findViewById(R.id.spelandesSaga); 
    t.setText(Html.fromHtml("<b>"+sagaRubrik+"</b>"));
}


private void startSaga(String url) 
{
    try {
        mp.reset();
        mp.setDataSource(url);
        mp.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    mp.start();

    myProgressBar=(ProgressBar)findViewById(R.id.mProgressDialog);

    new Thread(myThread).start();
}

private Runnable myThread = new Runnable(){
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (myProgress<100){
            try{
                myHandle.sendMessage(myHandle.obtainMessage());
                Thread.sleep(1000);
            }
            catch(Throwable t){
            }
        }
    }

    Handler myHandle = new Handler(){

        double poss = 0.0;
        double sagaleng = 0.0;

        @Override
        public void handleMessage(Message msg) {
            //  TODO Auto-generated method stub

            poss = mp.getCurrentPosition();
            sagaleng = mp.getDuration();

            progress = (int) ((int)poss / sagaleng * 100);

            myProgress = progress;
            myProgressBar.setProgress(progress);

        }
    };

};
public static String getFileFromUrl(String url) 
{
    InputStream content = null;
    try 
    {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpclient = new DefaultHttpClient();
        // Execute HTTP Get Request
        HttpResponse response = httpclient.execute(httpGet);
        content = response.getEntity().getContent();
    }
    catch (Exception e) 
    {
        showNoConnection ();
        return null;
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
    String line;
    StringBuilder sb =  new StringBuilder();
    try {
        while ((line = rd.readLine()) != null) {
                sb.append(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        rd.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sb.toString();


}
public boolean isOnline() {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

     boolean connected = cm.getActiveNetworkInfo().isConnectedOrConnecting();

     return connected;

}

private static void showNoConnection() 
{

}
private Runnable buffer = new Runnable(){
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (myProgress<100){
            try{
                myHandle.sendMessage(myHandle.obtainMessage());
                Thread.sleep(1000);
            }
            catch(Throwable t)
            {
            }
        }
    }
        Handler myHandle = new Handler(){


        @Override
        public void handleMessage(Message msg) {
            //  TODO Auto-generated method stub
            //SpelaPause.setImageURI("pauseicon");
            myBufferProgressBar = (ProgressBar)findViewById(R.id.mBuffrar);
            TextView bufferText = (TextView)findViewById(R.id.buffrarText);

            if (mp.isPlaying() && progress == 0)
            {
                myBufferProgressBar.setVisibility(View.VISIBLE);
                bufferText.setVisibility(View.VISIBLE);
            }
            else
            {
                myBufferProgressBar.setVisibility(View.INVISIBLE);
                bufferText.setVisibility(View.INVISIBLE);
            }




        }
    };

};

}

Upvotes: 3

Views: 11577

Answers (5)

Ashraf Sousa
Ashraf Sousa

Reputation: 1

I believe it has something to do with the progress bar you use. I use one and it slows my app. Not sure if the progress bar must slow the app or there is another way to avoid this though.

Upvotes: -2

Tien
Tien

Reputation: 71

This code works, without responsiveness problems. But the buffering takes to long time so I need another solution. I'm posting some code if someone else have the same problem

package comunicera.se;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GodnattsagaTest1 extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button = (Button) findViewById(R.id.SpelaPause);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            selectDownloadOrPLay();
        }
    });
}
public void selectDownloadOrPLay() {
    Toast.makeText(getApplicationContext(), "Before ... ", Toast.LENGTH_SHORT).show();
    Saga.startSaga ();
    Toast.makeText(getApplicationContext(), "after ... ",   Toast.LENGTH_SHORT).show();
}
}
class Saga 
{
static MediaPlayer mp = new MediaPlayer();
static void startSaga() 
{

    new Thread(spelaSaga).start();
}

private static Runnable spelaSaga = new Runnable(){
    @Override
    public void run() {
        // TODO Auto-generated method stub


            try {
                mp.reset();
                mp.setDataSource("http://godnattsaga.nu/sagor/gratis/Fisken.mp3");
                mp.prepare();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            mp.start();

    }
};



}

Upvotes: 0

Moss
Moss

Reputation: 6012

You are using getFileFromUrl in your onCreate method. Your method just performs the download action, which in case could last some time. You should always move long running tasks into it's own thread and notify the UI thread only.

Consider never running big logic in the UI thread, the UI thread should only be responsible for UI stuff.

To download a file in an async manner try to use the AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 3

underwood
underwood

Reputation: 278

All long-lasting tasks should be run in separate thread, not in UI thread. You call getFileFromUrl(..) from onCreate(..) method. This cause hangings.

I recommend you not to do any time consuming task in onCreate(..) method. In general an activity won't be shown till onCreate(..) is finished.

Upvotes: 4

Gangnus
Gangnus

Reputation: 24464

  1. If your application uses internet, it is possible, that the phone has worse connection than your comp. For example, if they both run on the SAME WiFi, at the same point, phones are connected MUCH worse than PC. Slower connection - you have to wait...
  2. Read http://developer.android.com/guide/practices/design/responsiveness.html and http://developer.android.com/guide/practices/design/performance.html - VERY useful. For example, you will know the name of your problem - bad responsiveness (not performance) - for better further searches. :-)

Upvotes: 5

Related Questions