我很困惑。幾天前,這工作正常。我做了一些改變,現在它的表現非常怪異。System.currentTimeMillis()返回0
我第一次調用該函數時,它返回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();
}
});
}
把一個日誌裏面的函數初始化** startTime **並檢查它是否被調用.. – Renjith
我會的。但是它被調用,因爲loadUrl函數工作正常,URL加載正常。 – Asim
但你應該檢查它被調用的時間。像給出的答案可能是你** onkey()**處理程序將在** startTime **被初始化之前被觸發。 – Renjith