一些优化后台任务和元数据任务代码的技巧

时间:2012-09-02 19:15:55

标签: android android-intent ogg

请帮助优化我的代码 1)我想在后台处理我的媒体元数据,所以它不会崩溃我的应用程序。 2)在较慢的互联网连接上,如3g开始流媒体需要10秒以上,并且它一直停留在新闻事件上直到它播放音乐..所以有时我得到强制关闭对话框有等待或确定按钮强制关闭app所以我也不想发生这种情况。

我想让我的应用程序使用更少的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);
    }

1 个答案:

答案 0 :(得分:0)

使用

mp.prepareAsync();

而不是

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