请帮助优化我的代码 1)我想在后台处理我的媒体元数据,这样它就不会让我的应用程序崩溃。2)在较慢的互联网连接(如 3g)上,开始流式传输需要超过 10 秒,并且它停留在该按下事件上,直到它播放音乐..所以有时我得到强制关闭对话框,其中有等待或确定按钮强制关闭应用程序所以我也不想发生这种情况。
我想让我的应用程序使用更少的 cpu 和更少的内存......
因此,请查看我的代码并给我示例链接,因为我是开发新手。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
// this is to take metadata from stream so i want do this part in separate task or in background
try
{
IcyStreamMeta icy = new IcyStreamMeta(new URL("http://84.15.135.51:80/mp3/adw"));
data = icy.getArtist() + " - " + icy.getTitle();
String temp = null;
runOnUiThread(new Runnable()
{
public void run()
{
tv.setText(data);
}
});
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
},0,20000);
mynotification(); // i called notification function to show notification
// i have used Vitamio jar and plugin to play ogg stream
try {
mp = new MediaPlayer(this);
} catch (VitamioNotCompatibleException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (VitamioNotFoundException e1) {
System.out.println("Install vitamio on your device");
Intent i = new Intent(this,mp3.class);
startActivity(i);
// TODO Auto-generated catch block
e1.printStackTrace();
}
// i want to play media streaming on background and in different task or service so it wont stuck during play .
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
swap();
try {
Boolean flag=mp.isPlaying();
if(flag==true)
{
mp.reset();
}
mp.setDataSource(link1); mp.setWakeMode(getApplicationContext(),
PowerManager.SCREEN_DIM_WAKE_LOCK);
mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
progressDialog.dismiss();
mp.start();
}
});
} 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();
}
}
});@Override
public void onBackPressed()
{
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
return;
}
// I am taking metadata data like title of track and show on notification.i want to show continuous notification until app exit
@SuppressWarnings("deprecation")
void mynotification()
{
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
String title = "radio";
Notification n = new Notification(R.drawable.noti, data, System.currentTimeMillis());
n.setLatestEventInfo(this , title, data, pi);
n.defaults=Notification.DEFAULT_ALL;
nm.notify(uniid,n);
}