0

如果没有可用的互联网连接或由于响应错误而发生任何错误,我想在显示消息后自动从启动屏幕关闭我的应用程序。我的代码关闭了应用程序,但无法关闭启动画面。印度时报(TOI)应用程序就是这样做的。如何实现此功能。

我的闪屏活动是这样的..

public class SplashScreen extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 8000;
static String MENU = null;
ArrayList<String> ls = new ArrayList<String>();
private String[] categoryType;
private boolean flag = true;

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

    // requesting data for menu items in navigation drawer 

    String url = "http://guwahatinow.com/?json=get_category_index";
    if (isOnline()) {
        JsonObjectRequest jsonObjReqMenu = new JsonObjectRequest(Method.GET,
                url, null, new Response.Listener<JSONObject>() {

            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArrayMenu= response.getJSONArray("categories");
                    Log.d("request", "menu");
                    int loop;
                    ls.add("Top Stories");
                    for (loop = 0; loop <jsonArrayMenu.length() ; loop++) {

                        JSONObject jsonObj = (JSONObject) jsonArrayMenu.get(loop);
                        String category =jsonObj.getString("title") ;
                        //menu.add(category);
                        ls.add(loop+1, category);
                        Log.d("menu added", category);
                        Log.d("element in ls", ls.get(loop));
                    }

                    ls.add("Exit");

                    int i = ls.size();
                    categoryType = new String[i];
                    for (int j = 0; j < i; j++) {
                        categoryType[j] = ls.get(j);
                    }

                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },  
        new Response.ErrorListener()  {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Please check your internet connection and try again...", Toast.LENGTH_LONG).show();
                VolleyLog.d("menu error", "Error: " + error.getMessage());
                flag = false;
                //finish();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
                //System.exit(0);

            }
        });


        RequestQueue menuQueue = Volley.newRequestQueue(this);
        menuQueue.add(jsonObjReqMenu);

        if (flag) {
            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(SplashScreen.this, MainActivity.class);
                    i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                    startActivity(i);

                    Log.d("main activity called", "true");
                    // close this activity
                    finish();
                }
            }, SPLASH_TIME_OUT);

        } else {
            Toast.makeText(getApplicationContext(), "Internet connection error...", Toast.LENGTH_LONG).show();
            finish();
            System.exit(0);
        }
        /*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(SplashScreen.this, MainActivity.class);
                i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                startActivity(i);

                Log.d("main activity called", "true");
                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);*/

    } else {
        Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();

    }
}

protected boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        return false;
    }
}

}
4

3 回答 3

0

嗨,请将您的代码替换为您使用处理程序线程的以下代码

CountDownTimer m_countDownTimer;
m_countDownTimer = new CountDownTimer(8000,1000) {
            @Override
            public void onTick(long millisUntilFinished)
            {

            }

            @Override
            public void onFinish()
            {
                if(!isFinishing())
                {
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    i.putExtra(com.hamburger.menu.SplashScreen.MENU, categoryType);
                    startActivity(i);

                    finish();
                }
            }
        }.start();

在 OnDestroy() 中取消 CountDownTimer 对象

@Override
    protected void onDestroy()
    {
        if(m_countDownTimer != null)
        {
            m_countDownTimer.cancel();
        }
        super.onDestroy();
    }
于 2015-09-16T11:40:02.100 回答
0

在 if 块的主要 else 块中,用于检查 Internet 调用完成()方法。例如

你有你的代码

if (isOnline()) {

//  your Code for calling API and starting timer 

} else {
  Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();
}

将其更改为

if (isOnline()) {

//  your Code for calling API and starting timer 

} else {
  Toast.makeText(getApplicationContext(), "Please connect to internet...", Toast.LENGTH_LONG).show();
  finish();
}
于 2015-09-16T11:08:40.190 回答
0

当您的情况或您想要关闭应用程序的任何其他地方没有可用连接时,在 Activity 上调用finish()方法。

于 2015-09-16T10:56:44.460 回答