I have a line in my class that is supposed to make a call to another class. I put a break point on this line and that break point is never reached, instead I get an error saying:
Can't create handler inside thread that has not called Looper.prepare( )
I have placed my code below. I am getting the error on the line where I try to call the AppTimeUsageClass
.
public void storeAppTimeUsageData(AppTimeUsage stats) {
//Context context = this;
List<AppTimeUsage> items = new ArrayList<>();
try {
appTimeUsageDao.insertOrReplace(stats);
} catch (Exception e) {
Log.e("Error", "Some exception occurred", e);
Log.e("APP_TAG", "STACKTRACE");
Log.e("APP_TAG", Log.getStackTraceString(e));
}
String sql = "SELECT * FROM APP_TIME_USAGE ";
Cursor c = appTimeUsageDao.getDatabase().rawQuery(sql, null);
int offset = 0;
int d ;
int cd ;
String e = "";
while (c.moveToNext()) {
AppTimeUsage atu = new AppTimeUsage(
c.getLong(0),
new Date (),
d = c.getInt(2),
e = c.getString(3)
// break;
);
items.add(atu);
stats = atu ;
}
UploadAppTimeUsageActivity aapTimeClass = new UploadAppTimeUsageActivity();
aapTimeClass.postAppTimeUsage(stats);
}
This is the code of the service that calls the "storeAppTimeUsageData" method
private boolean addToStatistics(String target )
{
Context context = this;
SQLiteOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), Globals.DatabaseName, null);
DaoMaster master = new DaoMaster(helper.getWritableDatabase());
daoSession = master.newSession();
appTimeUsageDao = DeviceInsightApp.getSession(this, true).getAppTimeUsageDao();
AppTimeUsage appStats;
boolean changed = false;
Date now = new Date();
if(!TextUtils.isEmpty(target))
{
if(!target.equals(foreground))
{
int i;
// timeCheckVariable = i ;
if(foreground != null && split != null)
{
// TODO: calculate time difference from current moment
// to the moment when previous foreground process was activated
i = packages.indexOf(foreground);
timeCheckVariable = i ;
long delta = (now.getTime() - split.getTime()) / 1000;
Long time = (Long)processList.get(i).get(ProcessList.COLUMN_PROCESS_TIME);
if(time != null)
{
// TODO: add the delta to statistics of 'foreground'
time += delta;
}
else
{
time = new Long(delta);
}
processList.get(i).put(ProcessList.COLUMN_PROCESS_TIME, time);
int x = time.intValue( );
String applicationName = (String)processList.get(i).get(ProcessList.COLUMN_PROCESS_NAME);
appStats = new AppTimeUsage(null , new Date(), x ,applicationName);
**//Call to storeAppTimeUsageData below**
new DataUsageRecorder(context).storeAppTimeUsageData(appStats);
}
//update count of process activation for new 'target'
i = packages.indexOf(target);
Integer count = (Integer)processList.get(i).get(ProcessList.COLUMN_PROCESS_COUNT);
if(count != null) count++;
else
{
count = new Integer(1);
}
processList.get(i).put(ProcessList.COLUMN_PROCESS_COUNT, count);
foreground = target;
split = now;
changed = true;
}
}
//Long checkTimeNow = (Long)processList.get(timeCheckVariable).get(ProcessList.COLUMN_PROCESS_TIME);
return changed;
}