2011-05-06 46 views
2

为什么这个程序停止回答,然后我按播放?它有时会显示“应用程序没有响应”,但如果我等待,它会起作用。这个应用程序为什么运行缓慢?

它适用于我的模拟器,但不适用于我的手机(或任何其他我试过的手机)。

它所做的只是流式声音。

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); 
      } 




     } 
    }; 

}; 

}

回答

5
  1. 如果应用程序使用互联网,这是可能的,手机有比你更差补偿连接。例如,如果他们都在同一个WiFi上运行,那么在同一时刻,手机的连接要比PC差得多。较慢的连接 - 你必须等待...
  2. 阅读http://developer.android.com/guide/practices/design/responsiveness.htmlhttp://developer.android.com/guide/practices/design/performance.html - 非常有用。例如,您将知道您的问题的名称 - 响应不良(不是性能) - 以便更好地进一步搜索。 :-)
4

所有长效任务应该在单独的线程中运行,而不是在UI线程中运行。您可以通过onCreate(..)方法调用getFileFromUrl(..)。这导致绞刑。

我建议你不要在onCreate(..)方法中做任何耗时的任务。一般情况下,一个活动将不会显示,直到onCreate(..)完成。

2

您在onCreate方法中使用getFileFromUrl。你的方法只是执行下载操作,这可能会持续一段时间。您应该始终将长时间运行的任务移动到它自己的线程中,并仅通知UI线程。

考虑从未在UI线程中运行大逻辑,UI线程应该只负责UI的东西。

要下载文件的异步方式尝试使用的AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html

0

此代码的工作,不响应的问题。但缓冲需要很长时间,所以我需要另一种解决方案。 我张贴一些代码,如果别人有同样的问题

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(); 

    } 
}; 



} 
-2

我相信这是与你使用的进度条。我使用一个,它会减慢我的应用程序。不知道进度条是否必须减慢应用程序,或者有另一种方法来避免这种情况。

相关问题