我很困惑。几天前,这工作正常。我做了一些更改,现在它的行为非常奇怪。
我第一次调用该函数时,它返回 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();
}
});
}