我试图获取任何应用程序的新鲜度值,即之前启动它的时间。为此我在 MyBroadcastReceiver 类的 onReceive(上下文上下文,Intent intent)函数内计算下面代码给出的时间。
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
String[] dateandtime= formattedDate.split(" ");
String mydate = dateandtime[0];
String mytime = dateandtime[1];
Toast.makeText(context.getApplicationContext(), formattedDate, Toast.LENGTH_SHORT).show();
在此之后,我将信息存储在函数末尾的SQL数据库中,如下所示
MyDatabaseHelper myDb;
myDb = new MyDatabaseHelper(context);
boolean r = myDb.insertData(mytime,lat ,lon , mydate, weekday, myNetwork, nameWIFI , audiojack, powersaver, appfullname );
if(r == true){
Toast.makeText(context.getApplicationContext(),"data inserted",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context.getApplicationContext(),"data not inserted",Toast.LENGTH_LONG).show();
}
我希望从应用程序启动之前到再次启动时获得时间差异。你能告诉我怎么做吗?
答案 0 :(得分:0)
您可以执行以下操作
@Override
public void onReceive(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the system time
long currentTime = System.currentTimeMillis();
// Store latest time in DB;
storeTime(currentTime);
getTimeDiff(currentTime);
}
private storeTime(time) {
// Insert in DB
}
private long getTimeDiff(currentTime) {
long previousTime = <Fetch earlier saved data if any>;
return currentTime - previousTime; // this will return you milliseconds
}