Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在文本文件中存储应用程序使用统计信息?_Java_Android_Data Storage - Fatal编程技术网

Java 如何在文本文件中存储应用程序使用统计信息?

Java 如何在文本文件中存储应用程序使用统计信息?,java,android,data-storage,Java,Android,Data Storage,我正在开发一个应用程序,它将提供安装在我手机上的使用统计信息,但我希望将这些统计信息(即应用程序打开的时间和时间等)存储在一个文件中,而不仅仅是显示在屏幕上 我希望应用程序在后台运行,并将应用程序的使用情况保存在文本文件中。您最好的选择是将此数据存储在共享首选项或类似数据库中。您最好的选择是将此数据存储在共享首选项或类似数据库中。我也提供与您相同的内容,但对我来说,这是家长控制应用程序。所以我展示了用于存储应用程序历史记录的方法 private void history(Accessib

我正在开发一个应用程序,它将提供安装在我手机上的使用统计信息,但我希望将这些统计信息(即应用程序打开的时间和时间等)存储在一个文件中,而不仅仅是显示在屏幕上


我希望应用程序在后台运行,并将应用程序的使用情况保存在文本文件中。您最好的选择是将此数据存储在共享首选项或类似数据库中。

您最好的选择是将此数据存储在共享首选项或类似数据库中。

我也提供与您相同的内容,但对我来说,这是家长控制应用程序。所以我展示了用于存储应用程序历史记录的方法

    private void history(AccessibilityEvent event) {
    SharedPreferences sf = getSharedPreferences("TempApp", MODE_PRIVATE);
    SharedPreferences.Editor editor = sf.edit();

        Log.i("eventSource", event.getPackageName().toString());
        try {
            appName = getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(event.getPackageName().toString(), PackageManager.GET_META_DATA)).toString();
            Log.i("CurrentApplication", appName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String tempApp = sf.getString("appName", "");
        if (tempApp.equals(appName)) {
            Log.i("Current", "inside if ");
        } else {
            editor.putString("appName", appName).apply();
            Calendar cl = Calendar.getInstance();
            String time, hour, minute, second;
             hour = (cl.get(Calendar.HOUR) < 10) ? "0" + cl.get(Calendar.HOUR) : ""+ cl.get(Calendar.HOUR);
             minute = (cl.get(Calendar.MINUTE) < 10) ? "0" + cl.get(Calendar.MINUTE) : ""+ cl.get(Calendar.MINUTE);
             second = (cl.get(Calendar.SECOND) < 10) ? "0" + cl.get(Calendar.SECOND) : ""+ cl.get(Calendar.SECOND);
             time = hour + ":" + minute + ":" + second;
            if (cl.get(Calendar.AM_PM) == 0)
                time += " AM";
            else
                time += " PM";
            int timeStart= cl.get(Calendar.HOUR_OF_DAY)*60*60+cl.get(Calendar.MINUTE)*60+cl.get(Calendar.SECOND);
            editor.putInt("nowTime",timeStart).apply();
            HistoryDB hDB = new HistoryDB(getApplicationContext());
            hDB.insertData(appName);
            hDB.update(tempApp, "EndTime", time);
            int timeUsed=sf.getInt("nowTime",0)-sf.getInt("prevTime",0);
            hDB.update(tempApp, "TimeUsed",String.valueOf(timeUsed));
            hDB.update(appName, "StartTime", time);
            editor.putInt("prevTime",timeStart).apply();
        }
    }
私有无效历史记录(AccessibilityEvent事件){
SharedPreferences sf=getSharedPreferences(“tempap”,模式_PRIVATE);
SharedReferences.Editor=sf.edit();
Log.i(“eventSource”,event.getPackageName().toString());
试一试{
appName=getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(event.getPackageName().toString(),PackageManager.GET_META_DATA)).toString();
Log.i(“当前应用程序”,appName);
}捕获(PackageManager.NameNotFounde异常){
e、 printStackTrace();
}
String tempApp=sf.getString(“appName”,”);
if(tempApp.equals(appName)){
日志i(“当前”、“内部if”);
}否则{
putString(“appName”,appName).apply();
Calendar cl=Calendar.getInstance();
字符串时间,小时,分钟,秒;
小时=(cl.get(Calendar.hour)<10)?“0”+cl.get(Calendar.hour):“”+cl.get(Calendar.hour);
分钟=(cl.get(Calendar.minute)<10)?“0”+cl.get(Calendar.minute):“”+cl.get(Calendar.minute);
秒=(cl.get(Calendar.second)<10)?“0”+cl.get(Calendar.second):“”+cl.get(Calendar.second);
时间=小时+“:”+分钟+“:”+秒;
如果(cl.get(Calendar.AM\u PM)==0)
时间+=“上午”;
其他的
时间+=“下午”;
int timeStart=cl.get(日历时间)×60*60+cl.get(日历分钟)×60+cl.get(日历秒);
putInt(“nowTime”,timeStart).apply();
HistoryDB hDB=新的HistoryDB(getApplicationContext());
hDB.insertData(appName);
hDB.更新(tempApp,“EndTime”,time);
int-timeUsed=sf.getInt(“nowTime”,0)-sf.getInt(“prevTime”,0);
hDB.update(tempApp,“TimeUsed”,String.valueOf(TimeUsed));
更新(appName,“StartTime”,time);
putInt(“prevTime”,timeStart).apply();
}
}
History.java

public class HistoryDB  extends SQLiteOpenHelper {
private static String DB_NAME = "History.db";
private static int DB_VR = 1;
private static String DB_TABLE = "HistoryData";
private static SQLiteDatabase dbb;
public HistoryDB(Context context) {
    super(context, DB_NAME, null, DB_VR);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table  " +DB_TABLE + "(AppName text,StartTime text ,EndTime text,TimeUsed Integer);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
void insertData(String appName)
{
    dbb = getWritableDatabase();
    dbb.execSQL("insert into "+ DB_TABLE + "(AppName,StartTime,EndTime,TimeUsed) values('"+appName+"',' ','                  ',0);");
    dbb.close();
}

 void update(String appName, String colName, String time) {
    dbb = getWritableDatabase();
    dbb.execSQL("update "+DB_TABLE+" set "+colName+"='"+time+"' where AppName='"+appName+"'");
    dbb.close();
}

 ArrayList<String> getData(String colName)
{
    dbb=getReadableDatabase();
    Cursor cr=dbb.rawQuery("select "+colName+" from "+DB_TABLE,null);
    ArrayList<String> arrayList=new ArrayList<>();
    while(cr.moveToNext()){
        arrayList.add(cr.getString(0));
    }
    dbb.close();
    cr.close();
    return arrayList;
}
公共类HistoryDB扩展了SQLiteOpenHelper{
私有静态字符串DB_NAME=“History.DB”;
私有静态int DB_VR=1;
私有静态字符串DB_TABLE=“HistoryData”;
私有静态sqlitedbb;
公共历史数据库(上下文){
super(上下文、数据库名称、null、数据库虚拟现实);
}
@凌驾
public void onCreate(SQLiteDatabase db){
execSQL(“创建表”+db_表+“(AppName文本、StartTime文本、EndTime文本、TimeUsed整数);”;
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
}
void insertData(字符串appName)
{
dbb=getWritableDatabase();
execSQL(“插入到”+DB_TABLE+”(AppName,StartTime,EndTime,TimeUsed)值(“+AppName+”,“,”,0);”;
dbb.close();
}
无效更新(字符串appName、字符串colName、字符串时间){
dbb=getWritableDatabase();
execSQL(“update”+DB_TABLE+“set”+colName+“=”“+time+”,其中AppName=”“+AppName+”);
dbb.close();
}
ArrayList getData(字符串colName)
{
dbb=getReadableDatabase();
游标cr=dbb.rawQuery(“从“+DB_表中选择“+colName+”,null);
ArrayList ArrayList=新的ArrayList();
while(cr.moveToNext()){
add(cr.getString(0));
}
dbb.close();
cr.close();
返回数组列表;
}

}

我也提供了和你一样的东西,但对我来说是家长控制应用程序。所以我展示了用于存储应用程序历史记录的方法

    private void history(AccessibilityEvent event) {
    SharedPreferences sf = getSharedPreferences("TempApp", MODE_PRIVATE);
    SharedPreferences.Editor editor = sf.edit();

        Log.i("eventSource", event.getPackageName().toString());
        try {
            appName = getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(event.getPackageName().toString(), PackageManager.GET_META_DATA)).toString();
            Log.i("CurrentApplication", appName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String tempApp = sf.getString("appName", "");
        if (tempApp.equals(appName)) {
            Log.i("Current", "inside if ");
        } else {
            editor.putString("appName", appName).apply();
            Calendar cl = Calendar.getInstance();
            String time, hour, minute, second;
             hour = (cl.get(Calendar.HOUR) < 10) ? "0" + cl.get(Calendar.HOUR) : ""+ cl.get(Calendar.HOUR);
             minute = (cl.get(Calendar.MINUTE) < 10) ? "0" + cl.get(Calendar.MINUTE) : ""+ cl.get(Calendar.MINUTE);
             second = (cl.get(Calendar.SECOND) < 10) ? "0" + cl.get(Calendar.SECOND) : ""+ cl.get(Calendar.SECOND);
             time = hour + ":" + minute + ":" + second;
            if (cl.get(Calendar.AM_PM) == 0)
                time += " AM";
            else
                time += " PM";
            int timeStart= cl.get(Calendar.HOUR_OF_DAY)*60*60+cl.get(Calendar.MINUTE)*60+cl.get(Calendar.SECOND);
            editor.putInt("nowTime",timeStart).apply();
            HistoryDB hDB = new HistoryDB(getApplicationContext());
            hDB.insertData(appName);
            hDB.update(tempApp, "EndTime", time);
            int timeUsed=sf.getInt("nowTime",0)-sf.getInt("prevTime",0);
            hDB.update(tempApp, "TimeUsed",String.valueOf(timeUsed));
            hDB.update(appName, "StartTime", time);
            editor.putInt("prevTime",timeStart).apply();
        }
    }
私有无效历史记录(AccessibilityEvent事件){
SharedPreferences sf=getSharedPreferences(“tempap”,模式_PRIVATE);
SharedReferences.Editor=sf.edit();
Log.i(“eventSource”,event.getPackageName().toString());
试一试{
appName=getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(event.getPackageName().toString(),PackageManager.GET_META_DATA)).toString();
Log.i(“当前应用程序”,appName);
}捕获(PackageManager.NameNotFounde异常){
e、 printStackTrace();
}
String tempApp=sf.getString(“appName”,”);
if(tempApp.equals(appName)){
日志i(“当前”、“内部if”);
}否则{
putString(“appName”,appName).apply();
Calendar cl=Calendar.getInstance();
字符串时间,小时,分钟,秒;
小时=(cl.get(Calendar.hour)<10)?“0”+cl.get(Calendar.hour):“”+cl.get(Calendar.hour);
分钟=(cl.get(Calendar.minute)<10)?“0”+cl.get(Calendar.minute):“”+cl.get(Calendar.minute);
秒=(cl.get(Calendar.second)<10)?“0”+cl.get(Calendar.second):“”+cl.get(Calendar.second);
时间=小时+“:”+分钟+“:”+秒;
如果(cl.get(Calendar.AM\u PM)==0)
时间+=“上午”;
其他的
时间+=“下午”;
int timeStart=cl.get(日历时间)×60*60+cl.get(日历分钟)×60+cl.get(日历秒);
putInt(“nowTime”,timeStart).apply();
HistoryDB hDB=新的HistoryDB(getApplicationContext());
hDB.insertData(appName);
hDB.更新(tempApp,“EndTime”,time);
int timeUsed=sf.getInt(“nowTime”,0)-sf.ge