如何在一段时间内异步调用与任务 [英] how to call Async task with in period of time

查看:123
本文介绍了如何在一段时间内异步调用与任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取音频元周期性像歌曲名和歌手名。
对于我使用的异步任务的媒体元数据猎犬。
所以,问题是

1)AsyncTask的类如何我可以将文本分配到TAXT视图。

2)我如何可以调用一个特定的时间内异步任务类,如30秒的时间间隔。

3)有一些文字,我需要在一天只有一次提取。所以如何可以检查最后一次的时候是数据获取?而且这是存储获取数据,并用它来ListView中最好的方法?(我是说我需要存储到该数据库或任何的HashMap或数组列表吗?)

下面是元数据retriver code

 公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    //充气的布局该片段
    返回inflater.inflate(R.layout.afragment,集装箱,FALSE);
}@覆盖
公共无效调用onStart(){
    super.onStart();
    initfrag();
}私人无效initfrag(){
    // TODO自动生成方法存根
    LV1 =(ListView控件)getView()findViewById(R.id.list)。    电视=(TextView中)getView()findViewById(R.id.tv1)。
    TV1 =(TextView中)getView()findViewById(R.id.tv2)。
    LongOperation任务=新LongOperation();
    task.execute();
}类LongOperation扩展的AsyncTask<弦乐,太虚,字符串> {
    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        MediaMetadataRetriever metaRetriever =新MediaMetadataRetriever();
        字符串link =HTTP://xyz-radio-link.ogg
        metaRetriever.setDataSource(链接,新的HashMap<字符串,字符串>());
        //获取MP3信息
        标题= metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
        艺术家= metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        metaRetriever.release();
        refreshtext();
        返回null;
    }
    @覆盖
    保护无效onPostExecute(字符串结果){
    }
    @覆盖
    在preExecute保护无效(){
    }
    @覆盖
    保护无效onProgressUpdate(虚空......值){
    }
}公共无效refreshtext(){
    // TODO自动生成方法存根
    字体TF = Typeface.createFromAsset(getActivity()getAssets(),字体/的Roboto-Regular.ttf);
    tv.setTypeface(TF);
    tv1.setTypeface(TF);
    tv.setText(艺术家);
    tv1.setText(职称);
}


解决方案

  

1)AsyncTask的类如何我可以将文本分配到TAXT视图。


您不能因为你是不是在UIThread运行。恕我直言,最好的做法是返回使用此值的UIThread onPostExecute()。另一种选择是使用 runOnUIThread()用自己的的Runnable 类。


  

2)我如何可以调用一个特定的时间内异步任务类,如
  30秒的时间间隔。


您可以使用 ScheduledExecutorService的


  

3)有一些文字,我需要在一天只有一次提取。所以
  如何检查,当最后一次数据抓取?


保存最后一次进入你的数据库


  

最好的方法来存储获取数据,并用它的ListView?(我
  意味着我需要到存储到数据库或任何的HashMap或数组
  名单?)


子类(扩展) ArrayAdapter 根据自己的需要,那么你可以直接用它来喂的ListView 这里的一个例子。

I want to fetch audio metadata periodically like song name and artist name. for that i am using media metadata retriever in Async-task. So problems are

1) In AsyncTask class how can i assign text to Taxt-view.

2) How can i call that Async-task class within a specific time, like 30sec time interval.

3) There are some text which i need to fetch only once in a day. so how can check that when last time data was fetched? and also which is the best method to store that fetch data and use it to Listview?(i mean i need to store that that into database or any hashmap or array list?)

Here is metadata retriver code

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.afragment, container, false);
}

@Override
public void onStart() {
    super.onStart();
    initfrag();
}

private void initfrag() {
    // TODO Auto-generated method stub
    lv1=(ListView)getView().findViewById(R.id.list);

    tv=(TextView)getView().findViewById(R.id.tv1);
    tv1=(TextView)getView().findViewById(R.id.tv2);
    LongOperation task=new LongOperation();
    task.execute();
}

class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
        String link = "http://xyz-radio-link.ogg";
        metaRetriever.setDataSource(link, new HashMap<String, String>());
        // get mp3 info
        title = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
        artist = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        metaRetriever.release();  
        refreshtext();
        return null;
    }      
    @Override
    protected void onPostExecute(String result) {               
    }
    @Override
    protected void onPreExecute() {
    }
    @Override
    protected void onProgressUpdate(Void... values) {       
    }
}

public void refreshtext() {
    // TODO Auto-generated method stub
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Regular.ttf");
    tv.setTypeface(tf);
    tv1.setTypeface(tf);
    tv.setText(artist);
    tv1.setText(title);
}

解决方案

1) In AsyncTask class how can i assign text to Taxt-view.

You can't because you are not running on UIThread. IMHO best practice is to return this value to the UIThread using onPostExecute(). Another choice is to use runOnUIThread() with your own Runnable class.

2) How can i call that Async-task class within a specific time, like 30sec time interval.

You can use ScheduledExecutorService.

3) There are some text which i need to fetch only once in a day. so how can check that when last time data was fetched?

Save the last time into your DB.

the best method to store that fetch data and use it to Listview?(i mean i need to store that that into database or any hashmap or array list?)

Subclass (extend) ArrayAdapter for your own needs, then you can directly use it to feed the ListView. Here's an example.

这篇关于如何在一段时间内异步调用与任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆