通过AsyncTask启动屏幕

时间:2015-11-17 15:03:47

标签: android android-asynctask

我需要那个,直到xml解析后台AsyncTask方法完成后才会显示一个启动画面,所以我用内部AsyncTask类创建了Loadingscreen活动,并在onPostExecute()上我用意图传递给mainactivity但它不起作用,启动画面是没有显示。

loadingactivity:

public class LoadingPage extends Activity
{    static ArrayList<WebPage> wps; 
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    //new getParseData().execute();

    try 
    {
        wps=new getParseData().execute().get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

class getParseData extends AsyncTask<Void ,Void,ArrayList<WebPage>>
{
     ArrayList<WebPage>wps;

    @Override
    protected ArrayList<WebPage> doInBackground(Void... params) 
    {

        WebPage ynet= new WebPage(R.drawable.ynet,    "http://www.ynet.co.il/Integration/StoryRss2.xml","http://www.ynet.co.il/Integration/StoryRss6.xml","http://www.ynet.co.il/Integration/StoryRss3.xml");
        WebPage walla=new WebPage(R.drawable.walla, "http://rss.walla.co.il/?w=/1/0/12/@rss.e","http://rss.walla.co.il/?w=/2/0/12/@rss.e","http://rss.walla.co.il/?w=/3/0/12/@rss.e");
        WebPage  nrg= new WebPage(R.drawable.maariv,   "http://rss.nrg.co.il/news/","http://rss.nrg.co.il/finance","http://rss.nrg.co.il/sport");

        ArrayList <WebPage>wps=new ArrayList<WebPage>();

        wps.add(ynet);
        wps.add(walla);
        wps.add(nrg);
        boolean coteretfilled=false;
        boolean linkfilled=false;
        boolean imgfilled=false;
        for(int y=0;y<wps.size();y++)
        {
            WebPage currentwp=wps.get(y);

            for(int i=0;i<currentwp.urls.length;i++)
            {
                ArrayList<TitlesVieLinks> tl=new ArrayList<TitlesVieLinks>();
                String urlString=currentwp.urls[i];
                try 
                {
                    URL url=new URL(urlString);
                    XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
                    XmlPullParser parser=factory.newPullParser();
                    InputStream is=url.openStream();
                    parser.setInput(is,null);
                    boolean item=false;
                    boolean title=false;
                    boolean link=false;
                    boolean description=false;
                    String coteret="";
                    String _link = "";
                    //String _imglink ="";
                    String tagname="";
                    String result="";
                    int eventype=parser.getEventType();
                    while(eventype!=XmlPullParser.END_DOCUMENT)
                    {  
                        if(eventype==XmlPullParser.START_TAG)
                        {  
                            tagname=parser.getName();
                            if(item)
                            {
                                if(tagname.equals("title"))
                                    title=true;
                                if(tagname.equals("link"))
                                    link=true;
                                if(tagname.equals("description"))
                                    description=true;
                            }
                            else//not item
                            {
                                if(tagname.equals("item"))
                                    item=true;
                            }
                        }
                        if(eventype==XmlPullParser.END_TAG)
                        {
                            tagname=parser.getName();
                            if(tagname.equals("item"))
                                item=false;
                        }
                        if(eventype==XmlPullParser.TEXT)
                        {
                            if(title)
                            {
                                coteret=parser.getText();
                                coteretfilled=true;
                                title=false;
                            }
                            if(link)
                            {
                                _link=parser.getText();
                                linkfilled=true;
                                link=false;
                            }
                            if(description)
                            {
                                String cdata = parser.getText();
                                Log.i("Info", cdata);

                                int idxStart = cdata.indexOf("src=");
                                if (idxStart > 0)
                                {
                                    int idxEnd = Math.max(cdata.indexOf(".jpg"), cdata.indexOf(".png"));//returns the greater of 2 int values 
                                    if (idxEnd > 0)
                                    {
                                        result = cdata.substring(idxStart+5, idxEnd+4);                     
                                    }
                                }
                                else
                                    result="";
                                Log.i("InfoResult", result);

                                imgfilled=true;
                                description=false;
                            }
                            if(coteretfilled && linkfilled && imgfilled)
                            {
                                TitlesVieLinks titlink=new TitlesVieLinks(coteret, _link,result);   
                                tl.add(titlink);
                                coteretfilled=false;
                                linkfilled=false;
                                imgfilled=false;
                            }
                        }
                        eventype = parser.next();
                    }//end of while loop 
                    if(i==0)
                    {
                        currentwp.NewsTitles.addAll(tl);
                    }
                    else if(i==1)
                    {
                        currentwp.EconomicsTitles.addAll(tl);
                    }
                    else if(i==2)
                    {
                        currentwp.SportssTitles.addAll(tl);
                    }
                    tl.clear();
                } 
                catch (MalformedURLException e) 
                {
                    e.printStackTrace();
                } catch (XmlPullParserException e) 
                {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }// end of inner for loop
        }//end of first loop
        Log.i("every thing is cool", wps.get(0).NewsTitles.size()+"");
        return wps;
    }
    @Override
    protected void onPostExecute(ArrayList<WebPage> result) 
    {
        super.onPostExecute(result);
        Intent i=new Intent(LoadingPage.this,NewsMainActivity.class);
        startActivity(i);
        finish();
    }
    @Override
    protected void onPreExecute() {}
}

mainactivity:

 public class NewsMainActivity extends Activity 
 {
      static ArrayList<WebPage>wps;
      WebPage wp;
      NewsListAadpter adapter;
      public static final int YNET_WP_POS = 0;
      public static final int WALLA_WP_POS = 1;
      public static final int NRG_WP_POS = 2;

     ListView newsListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //set content view AFTER ABOVE sequence (to avoid crash)           
    setContentView(R.layout.activity_news_main);
    wps= LoadingPage.wps;
    adapter=new NewsListAadpter(this);
    fillNews( R.drawable.ynet, YNET_WP_POS, adapter);
    fillNews( R.drawable.walla, WALLA_WP_POS, adapter);
    fillNews( R.drawable.maariv, NRG_WP_POS, adapter);

    newsListView=(ListView)findViewById(R.id.newsListView);
    newsListView.setAdapter(adapter);
}

     private void fillNews(int drawableId, int position, NewsListAadpter adapter)//this func is used in the onpost     
{
      adapter.addImageSeperator(drawableId, position);
      adapter.addSeparatorItem("חדשות");
      for (int i = 0; i < 3; i++) {
        adapter.addItem(wps.get(position).getNewsTitles().get(i));      //3    first titles from news       
    }
        adapter.addLink(drawableId, position, 0, "עוד בחדשות");
        adapter.addSeparatorItem("ספורט");
        for (int i = 0; i < 3; i++) {
        adapter.addItem(wps.get(position).getSportssTitles().get(i));               
    }
        adapter.addLink(drawableId, position,1, "עוד בספורט");
        adapter.addSeparatorItem("כלכלה");
        for (int i = 0; i < 3; i++) {
        adapter.addItem(wps.get(position).getEconomicsTitles().get(i));             
    }
        adapter.addLink(drawableId, position, 2, "עוד בכלכלה");
}

清单:

  <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="com.ravbm.newsreader"
           android:versionCode="1"
           android:versionName="1.0" >
<uses-sdk
     android:minSdkVersion="3"
     android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
      android:allowBackup="true"
      android:icon="@drawable/news_icon"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
    <activity
         android:name="com.ravbm.newsreader.NewsMainActivity"
         android:configChanges="orientation|keyboardHidden|screenSize"
         android:label="@string/app_name"
         android:theme="@style/FullscreenTheme" >
        <intent-filter>

        </intent-filter>
    </activity>
    <activity
         android:name="com.ravbm.newsreader.AllNewsActivity"
         android:configChanges="orientation|keyboardHidden|screenSize"
         android:label="@string/title_activity_all_news"
         android:theme="@style/FullscreenTheme" >
    </activity>
    <activity android:name="com.ravbm.newsreader.LoadingPage"
               >
         <intent-filter>
               <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我尝试了而不是打电话:

wps=new getParseData().execute().get();

该应用程序运行良好,但没有显示加载页面,而是有一个空白页,所以我试图调用asynctask而没有get:

new getParseData().execute();

然后会发生加载页面,但应用程序会立即崩溃。 请问有什么不对的。

2 个答案:

答案 0 :(得分:1)

好的,就像我在评论中所说的那样,这将是一个实际上没有回答你的问题的答案,但是通过以更稳定的方式提供你正在寻找的相同功能来回答你的问题。这是因为如果您的LoadingPage活动崩溃或引发ANR,您的应用将退出/退出,但是当您AsyncTask完成后,即使您的应用仍然会启动NewsMainActivity已经退出了......那是不好的mkay:)

但要小心谨慎;确保您的Webpage类(您要返回onPostExecute()方法的类)实现Parcelable。这比使用全局静态变量更简单,更简洁,只是希望它始终有效。这样做是非常可能的,而不是错误的,但这不是简洁的代码。

这些代码示例将说明您必须如何实施IntentServiceResultReceiver,但您自己也必须做一些工作。让我们从构建IntentService

开始
public class LoadingService extends IntentService {
    public static final String RECEIVER_KEY = "receiver";
    public static final String SUCCESS_KEY = "success";
    public static final int SUCCESS_CODE = 0;
    public static final String ERROR_KEY = "error";
    public static final int ERROR_CODE = 1;

    public LoadingService() {
        super("LoadingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        final ResultReceiver receiver = intent.getParcelableExtra(RECEIVER_KEY);
        final Bundle result = new Bundle();

        if (receiver == NULL) return;

        try {
            /*
            So what you wanna do here is everything you currently do in doInBackground()
            It's very probably just as simple as copying it over and executing it here
            */

            /*
            These next two lines are only needed if your Webpage class implements Parcelable
            In that case we give it back to the LoadingPage Activity

            Now if you're not going the Parcelable route, replace the following two lines with
            code to put the Webpages into your global static variable
            */
            result.putParcelableArrayList(SUCCESS_KEY, /*NAME OF YOUR WEBPAGES LIST HERE*/);
            receiver.send(SUCCESS_CODE, result);
        } catch (Exception e) {
            result.putString(ERROR_KEY, "Something went wrong: " + e.getMessage());
            receiver.send(ERROR_CODE, result);
        }
    }
}

现在我们必须在我们的Manifest中注册它,所以只需添加以下内容

<service name=".LoadingService" />

现在,这里是如何从您的启动活动启动它并接收结果,然后启动NewsMainActivity

public class LoadingPage extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        //This starts the loading service and calls mReceiver when done
        Intent intent = new Intent(LoadingPage.this, LoadingService.class);
        intent.putExtra(LoadingService.RECEIVER_KEY, mReceiver);
        startService(intent);
    }

    final ResultReceiver mReceiver = new ResultReceiver(new Handler()) {
        @Override
        protected void onResultReceived(int resultCode, Bundle resultData) {
            if (resultCode == LoadingService.SUCCESS_CODE) {
                //THIS PART IS ONLY USEFUL WHEN WEBPAGE IMPLEMENTS PARCELABLE
                final ArrayList<Webpage> webpages = resultData.getParcelableArrayList(
                        LoadingService.SUCCESS_KEY);
                if (webpages != null && webpages.size > 0) {
                    //HERE YOU START YOUR MAIN ACTIVITY WITH THE WEBPAGES WE RECEIVED
                    Intent intent = new Intent(LoadingPage.this, MainActivity.class);
                    intent.putParcelableArrayListExtra(MainActivity.WEBPAGES_KEY, webpages);
                    startActivity(intent);
                    finish();
                }

                //NOW IF WEBPAGE DOESN'T IMPLEMENT PARCELABLE, ONLY START THE ACTIVITY
                //SO EITHER USE THE CODE ABOVE OR THE 1 LINE BELOW
                startActivity(new Intent(LoadingPage.this, MainActivity.class));
                finish();
            } else if (resultCode == LoadingService.ERROR_CODE) {
                final String error = resultData.getString(LoadingService.ERROR_KEY);
                //DISPLAY AN ERROR MESSAGE HERE, LIKE: COULD NOT RETRIEVE DATA
            }
        }
    }
}

LoadingPage启动加载服务,该服务执行您需要执行的后台操作,然后在完成或遇到错误时向ResultReceiver发送信号。在ResultReceiver中,我们会检查结果是SUCCESS还是ERROR。如果我们取得了成功,它将启动以下Activity

public class NewsMainActivity extends Activity {
    public static final String WEBPAGES_KEY = "webpages";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_main);

        final ArrayList<Webpage> webpages = getIntent()
                .getParcelableArrayList(WEBPAGES_KEY);
        //DO WHATEVER YOU NEED TO DO HERE :)
    }
}

那应该是它!如果您有任何疑问,我很乐意听到'em:)

答案 1 :(得分:0)

如果您提供日志,很容易找到崩溃的主要原因。 检查将finish()替换为LoadingPage.this.finish()