I’ve got several problems and I’m starting to wonder if my path is correct…
I would like to make a list of the most often used appliactions based on time scheduled measurments of processor usage. I know how to get usage statistics and processes names, I know how to connect it with apps names and icons. What I don’t know is what to do next.
My idea was to create two tables: PROCESSES AND PROCESS_STATS, store all the information in the database and provide ContentProvider linked with SimpleCursorAdapter. In my opinion it’s the cleaniest solution when working with SQLiteDB on Android. Don’t bother counting effects into some “real” statistics, it’s not the case here.
Problems:
- I can’t create the table with blob in it. Code in
listing 1does not work. When i try to read from table (using the code fromListing 2) I getjava.lang.IllegalArgumentException: column 'app_icon' does not exist. There are no errors in LogCat while creating the db. - Storing icons (bitmaps) in the db is not a very good design pattern. However, I like the simplicity of my solution: ContentProvider, SimpleCursorAdapter and Fragments do all the background work for me. If I’d like to store icons on SD i would have much more work, especially with the final list of apps. What are the other ways to do this painlessly?
Listing 1:
CREATE TABLE processes
(_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
app_name TEXT,
app_icon BLOB);
CREATE TABLE process_stats
(_id INTEGER PRIMARY KEY AUTOINCREMENT,
process_id INTEGER NOT NULL,
FOREIGN KEY(process_id) REFERENCES processes(_id),
value INTEGER NOT NULL,
measurment_date INTEGER NOT NULL);
Listing 2:
...
import android.support.v4.widget.SimpleCursorAdapter;
...
public class ProcessesStatsActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = this.managedQuery(StatsProvider.PROCESSES_CONTENT_URI, null, null, null, null);
String[] columns = new String[] { StatsCollectorDatabase._PROCESS_APP_ICON, StatsCollectorDatabase._PROCESS_APP_NAME, StatsCollectorDatabase._PROCESS_STAT_VALUE };
int[] views = new int[] { android.R.id.icon, android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.processes_stats_list_item, cursor, columns, views, 0);
this.setListAdapter(adapter);
}
}
For future visitors:
I ended up with solution like this:
I don’t save application info in the database. I have only one table,
process_stats, in which I store measurment results. When it comes to view the results, I loop manually through the coursor and count everything I wanted. Next, I load details like app icon and app name and view the list.I can paste some sample code here. Comment this answer, if interested.