System.currentTimeMillis()返回0

时间:2012-09-26 09:54:26

标签: android time system

我很困惑。几天前,这个工作正常。我做了一些改变,现在它表现得很奇怪。

第一次调用该函数时,它返回0.第二次,在代码的后面,它返回正确的时间。

我在这里粘贴了大部分的类文件,所以你们可以诊断。

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.newsdetail);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
selectedNewsItem = newsList.get(_index);
            AlertDialog.Builder alert = new AlertDialog.Builder(NewsDetail.this);

            alert.setTitle(selectedNewsItem.getTitle());
            WebView wv = new WebView(NewsDetail.this);
            wv.getSettings().setUserAgentString("Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaC6-00/20.0.042; Profile/MIDP-2.1 Configuration/CLDC-1.1; zh-hk) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.2.6.9 3gpp-gba");
            wv.getSettings().setSupportZoom(false);
            wv.loadUrl(selectedNewsItem.getLink());
            wv.setWebViewClient(new WebViewClient()
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    view.loadUrl(url);
                    startTime = System.currentTimeMillis();
                    return true;
                }
            });

            alert.setView(wv);
            alert.setOnKeyListener(new OnKeyListener()
            {
                boolean didItHappen = false;
                int happencount = 0;
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                {
                    if(keyCode == event.KEYCODE_BACK)
                    {
                        happencount++;
                        endTime = System.currentTimeMillis();
                        Log.d("StartTime: ", String.valueOf(startTime));
                        Log.d("EndTime: ", String.valueOf(endTime));
                        totalTime = (endTime - startTime) / 1000;

                        if(happencount == 2)
                        {
                            Log.d("Time Spent: ", totalTime + " seconds");
                            didItHappen = true;
                        }
                    }
                    if(happencount == 2)
                    {
                        SharedPreferences shp;
                        SharedPreferences.Editor ed;                            
                        shp = getSharedPreferences("timespent", MODE_PRIVATE);
                        ed = shp.edit();

                        ed.putString("Title", selectedNewsItem.getTitle());
                        ed.putLong("Time", totalTime);
                        ed.putString("*****", "*****");                         
                        ed.commit();

                        startTime = 0;
                        endTime = 0;
                        totalTime = 0;
                        happencount = 0;
                    }

                    return false;
                }
            });
            alert.show();               
        }
    });
}

1 个答案:

答案 0 :(得分:3)

startTime?我不确定您在设置System.currentTimeMillis()时是否有startTime返回0的证据。更有可能的是shouldOverrideUrlLoadingonKey()处理程序之前没有被调用,这就是全部。