es mi primera app, y tengo el problema que al pulsar en el ListView
la app como que se congela un momento y luego responde, creo que es por que mi código esta mal optimizado, como lo puedo solucionar?
package com.david.animefm2;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.text.CollationElementIterator;
import java.util.ArrayList;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import static android.R.drawable.ic_media_play;
public class MainActivity extends Activity {
private ArrayList radios;
private RadioAdapter adapter;
private ListView lvRadios;
private String url ="";
private String txt = "";
private MediaPlayer mediaPlayer = new MediaPlayer();
private AdView mAdView;
TextView texto;
ImageView cover;
//noti
// Variables de la notificacion
NotificationManager nm;
Notification notif;
static String ns = Context.NOTIFICATION_SERVICE;
//Defino los iconos de la notificacion en la barra de notificacion
int icono_v = R.drawable.animelayer;
int icono_r = R.drawable.radioanime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView)findViewById(R.id.etRadio);
cover = (ImageView)findViewById(R.id.cover);
//config
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
MobileAds.initialize(this, "ca-app-pub-9173335460606469/5989305154");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Log.i("Ads", "onAdFailedToLoad");
}
@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
Log.i("Ads", "onAdOpened");
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Log.i("Ads", "onAdLeftApplication");
}
@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
Log.i("Ads", "onAdClosed");
}
});
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.start);
//variables
radios = new ArrayList<radio>();
rellenarArrayList();
adapter = new RadioAdapter(this, radios);
lvRadios = (ListView) findViewById(R.id.lvRadeo);
lvRadios.setAdapter(adapter);
//Lista
ListView lista = (ListView) findViewById(R.id.lvRadeo);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
SetUrl(i);
}
});
// Inicio el servicio de notificaciones accediendo al servicio
nm = (NotificationManager) getSystemService(ns);
// Realizo una notificacion por medio de un metodo hecho por mi
notificacion(icono_r, "titulo contenido", "texto contenido", "texto extendido");
//BOTON FLOTANTE
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()){
//metodo de pausa
mediaPlayer.stop();
mediaPlayer.reset();
} else {
BotonReproducir();
}
}
});
}
//Nombre y icono
private void rellenarArrayList() {
radios.add(new radio("Radio Vocaloid", R.drawable.radiovocaloid));
radios.add(new radio("Radio Anime", R.drawable.radioanime));
radios.add(new radio("Anime Layer", R.drawable.animelayer));
radios.add(new radio("Anime Radio SU", R.drawable.radioanimesu));
radios.add(new radio("canario", R.drawable.ic_home_black_24dp));
radios.add(new radio("cerdo", R.drawable.ic_home_black_24dp));
radios.add(new radio("delfin", R.drawable.ic_home_black_24dp));
radios.add(new radio("gato", R.drawable.ic_home_black_24dp));
radios.add(new radio("iguana", R.drawable.ic_home_black_24dp));
radios.add(new radio("lince", R.drawable.ic_home_black_24dp));
radios.add(new radio("lobo", R.drawable.ic_home_black_24dp));
radios.add(new radio("morena", R.drawable.ic_home_black_24dp));
radios.add(new radio("orca", R.drawable.ic_home_black_24dp));
radios.add(new radio("perro", R.drawable.ic_home_black_24dp));
radios.add(new radio("vaca", R.drawable.ic_home_black_24dp));
};
//Urls
private void seturl(int i) {
switch (i) {
case 0:
url= "http://curiosity.shoutca.st:8019/stream"; //Radio Vocaloid
txt = "Radio Vocaloid";
cover.setImageResource(R.drawable.radiovocaloid);
break;
case 1:
url="http://perseus.shoutca.st:9253/stream"; //Radio Anime
txt = "Radio Anime";
cover.setImageResource(R.drawable.radioanime);
break;
case 2:
url="http://animelayer.ru:5190/play"; //Anime Layer
txt = "Anime Layer";
cover.setImageResource(R.drawable.animelayer);
break;
case 3:
url="http://animeradio.su:8000"; //Radio Anime SU
txt = "Radio Anime SU";
cover.setImageResource(R.drawable.radioanimesu);
break;
};
texto.setText(txt);
BotonReproducir();
}
private void SetUrl(int i){
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset();
}
Toast.makeText(getApplicationContext(), "posicion " + (i), Toast.LENGTH_SHORT).show();
seturl(i);
}
private void BotonReproducir() {
if (url != "") {
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e1) {
e1.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
//Inicia reproducción.
mediaPlayer.start();
Toast.makeText(getApplicationContext(),"Bufering...", Toast.LENGTH_LONG ).show();
// Lanzo la notificacion creada en el paso anterior
nm.notify(1, notif);
//fab.setImageResource(R.drawable.ic_media_play);
}
});
} else {
Toast.makeText(getApplicationContext(),"Select a rario", Toast.LENGTH_SHORT).show();
}
}
//notificacion
public void notificacion(int icon, CharSequence textoEstado, CharSequence titulo, CharSequence texto) {
// Capturo la hora del evento
long hora = System.currentTimeMillis();
// Definimos la accion de la pulsacion sobre la notificacion (esto es opcional)
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Defino la notificacion, icono, texto y hora
notif = new Notification(icon, "Holaaaaa", hora);
// notif.(getApplicationContext(), "Old title", "Old notification content text", PendingIntent.getActivity(mContext, 0, new Intent(), 0));
//Defino que la notificacion sea permamente
//notif.flags = Notification.FLAG_ONGOING_EVENT;
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
nm.cancel(1);
}
@Override
public void onDestroy() {
super.onResume();
Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
nm.cancel(1);
}
public void onPause() {
super.onResume();
Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
nm.notify(1, notif);
}
}