【问题标题】:How to hide the file download with splash screen activity如何使用启动屏幕活动隐藏文件下载
【发布时间】:2014-03-22 23:43:10
【问题描述】:

我正在尝试实现一个应用程序,我必须首先下载一个文件,然后才能继续。所以我不想让屏幕在 下载期间闲置 N 秒,我想用启动屏幕覆盖它。所以基本上把所有的文件都下载下来了,就会被splash Activity覆盖了。

这是我一般用于启动活动的代码,然后在延迟后跳转到主要活动。

真正的问题是我在 main_activity 中有 AsyncTask,我想显示启动画面,而我可以在 AsyncTask 中下载文件。然后我可以转到主要活动

 new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity



                Intent i = new Intent(Splash_Screen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, 3000);
}

【问题讨论】:

  • Android ui 框架的作用你了解多少?
  • 你的意思是说!我的问题是错的吗?或..
  • 不,我是在询问你的背景,以便其他人或我知道如何详细回答。您在问题中提供的细节很少。
  • @VamsiPavanMahesh:到目前为止你有没有尝试过?如果是这样,请发布您的代码并解释您的问题。
  • 你可以认为我是中级程序员

标签: android android-activity splash-screen


【解决方案1】:

我建议您选择 AsyncTask,您可以在下载文件的示例中在后台执行任务,在下载文件后执行后执行意味着您可以执行什么操作随心所欲。

private class Asyn_DownLoadFile extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        //download your file here
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        /*this function is called automatically by the doInBackground when 
                    it finish it's work*/
    }

}

请记住 AsyncTask 必须是子类化的,这意味着调用在 Splash screen Activity 中实现 AsyncTask 并在 onPostExecution 中执行您想要的操作

使用 AsyncTask 启动

 new Asyn_DownLoadFile().execute(null,null,null);

编辑:

这里你必须做的:

假设这是你的 Splash 类

public class SplashActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    context = this;
    //.......................
    new Asyn_DownLoadFile().execute(null,null,null);
}

    //this is your function that downLoad the file
public void downLoadFile(){
     //............................
}

private class Asyn_DownLoadFile extends AsyncTask<Void, Void, Void> {
   @Override
   protected Void doInBackground(Void... params) {
       downLoadFile();
   }

   @Override
   protected void onPostExecute(Void result) {
       // TODO Auto-generated method stub
       super.onPostExecute(result);
       Intent i = new Intent (context, MainActivity.class)
       startActivity(i);
   }

}

}

【讨论】:

  • 我在主活动中有这个 AsyncTask,我现在有一个启动画面,那么我如何在下载文件并进入主活动时显示启动画面
  • @VamsiPavanMahesh 在PreExecute 上显示初始屏幕并在PostExecute 上隐藏它。
  • 请看 mu 编辑,我想 SplashActivity 是你的启动 @VamsiPavanMahesh 的活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多