一些优化后台任务和元数据任务代码的提示



请帮助优化我的代码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);
    }

使用

mp.prepareAsync();

而不是

mp.prepare(); // runs on main thread

最新更新