I am trying to store the called number whenever user makes an outgoing call.
First, number is checked with existing numbers in a log table present in local sqlitedb. If the number is present in log, then call information(called number, date-time of call and conversation time of the call) is to be stored in database.
Everything works fine, if the app is running in the background.
When, app is closed, a NullPointerException(for getReadableDatabase()) is thrown while trying to retrieve data from log table of numbers. I tried to figure out what am I possibly be doing wrong and I figured out (not very sure) that context required for the database class (where I am creating an instance to sqlite) is null when the app is closed.
I need to get over this issue and implement the functionality.
What I have tried:
Here is the code:
Class containing database functinalities
public class tool_db_functions extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "kaarigars";
private static final int DATABASE_VERSION = 2;
public final String TABLE_LOG_CUST_DIALLED_KAARIGAR = "log_cust_dialled_kaarigar";
public final String TABLE_LOG_CUST_CALLED_KAARIGAR = "log_cust_called_kaarigar";
public final String[] COL_LOG_CUST_DIALLED_KAARIGAR = {"kid"};
public final String[] COL_LOG_CUST_CALLED_KAARIGAR = {"kid", "scode", "logdate", "conversationtime"};
String CREATE_TABLE_LOG_CUST_DIALLED_KAARIGAR = "CREATE TABLE " + TABLE_LOG_CUST_DIALLED_KAARIGAR +
"(" + COL_LOG_CUST_DIALLED_KAARIGAR[0] + " INTEGER PRIMARY KEY " + ")";
String CREATE_TABLE_LOG_CUST_CALLED_KAARIGAR = "CREATE TABLE " + TABLE_LOG_CUST_CALLED_KAARIGAR +
"(" +
COL_LOG_CUST_CALLED_KAARIGAR[0] + " INTEGER NOT NULL, " +
COL_LOG_CUST_CALLED_KAARIGAR[1] + " INTEGER DEFAULT NULL, " +
COL_LOG_CUST_CALLED_KAARIGAR[2] + " DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, " +
COL_LOG_CUST_CALLED_KAARIGAR[3] + " TEXT NOT NULL " +
")";
private static tool_db_functions sInstance;
private tool_db_functions(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static synchronized tool_db_functions getInstance(Context context)
{
try
{
sInstance = new tool_db_functions(context.getApplicationContext());
return sInstance;
}
catch (NullPointerException ex)
{
sInstance = new tool_db_functions(context);
return sInstance;
}
}
@Override
public void onConfigure(SQLiteDatabase db)
{
super.onConfigure(db);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(CREATE_TABLE_LOG_CUST_DIALLED_KAARIGAR);
db.execSQL(CREATE_TABLE_LOG_CUST_CALLED_KAARIGAR);
}
catch (Exception e)
{
throw e;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (oldVersion != newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOG_CUST_DIALLED_KAARIGAR);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOG_CUST_CALLED_KAARIGAR);
onCreate(db);
}
}
public Cursor getData(String _query)
{
SQLiteDatabase db = sInstance.getReadableDatabase();
Cursor _cursor = db.rawQuery(_query, null);
return _cursor;
}
public int deleteFromLogCustCalledKaarigar(String[] _logDate)
{
int _rowDeleted = -1;
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try
{
_rowDeleted = db.delete(TABLE_LOG_CUST_CALLED_KAARIGAR, "where logdate = ?", _logDate);
db.setTransactionSuccessful();
}
catch (Exception e)
{
throw e;
}
finally
{
db.endTransaction();
return _rowDeleted;
}
}
}
Class from where called number is checked for presence in the database
public class tool_store_num_dialled_call_log
{
tool_db_functions _db = tool_db_functions.getInstance(new mainActivity());
public String checkForDialledNumberInDb(String _dialledNumber)
{
Cursor _cursor;
Log.e("Table name",_db.TABLE_LOG_CUST_DIALLED_KAARIGAR);
Log.e("DB",_db.toString());
_cursor = _db.getData("select count(kid) as kcount from " + _db.TABLE_LOG_CUST_DIALLED_KAARIGAR + " where kid = " + _dialledNumber);
try
{
if (_cursor.moveToFirst())
{
if (_cursor.getString(_cursor.getColumnIndex("kcount")).equals("0"))
return "false";
else if (_cursor.getString(_cursor.getColumnIndex("kcount")).equals("1"))
return "true";
else
return "Query returned unexpected results at checkForDialledNumberInDb at tool_store_num_dialled_call_log.java";
}
else
return "Unable to move cursor to the first position for checkForDialledNumberInDb at tool_store_num_dialled_call_log.java";
}
catch (Exception e)
{
return "Exception raised at checkForDialledNumberInDb at tool_store_num_dialled_call_log.java with message: " + e.getMessage();
}
finally
{
if (_cursor != null && !_cursor.isClosed())
_cursor.close();
}
}
Broadcast receiver class for outgoing calls
public class tool_broadcast_receiver_outgoing_call extends BroadcastReceiver
{
@Override
public void onReceive(Context _contex, Intent _intent)
{
final Bundle _bundle = _intent.getExtras();
try
{
if (_bundle != null)
{
String _calledNumber = _bundle.getString(Intent.EXTRA_PHONE_NUMBER);
if (_calledNumber.length() == 11)
_calledNumber = _calledNumber.substring(_calledNumber.length() - 10, 11);
else if (_calledNumber.length() == 13)
_calledNumber = _calledNumber.substring(_calledNumber.length() - 10, 13);
tool_store_knum_dialled_call_log _logDialledNumbers = new tool_store_knum_dialled_call_log();
String _flag = _logDialledNumbers.checkForDialledNumberInDb(_calledNumber);
if (_flag.equals("true"))
saveCallLogToLocalDB(_calledNumber);
else if (_flag.equals("error"))
Log.e("Error: ", "From onReceive at tool_broadcast_receiver_outgoing_call.java. Unable to check for dialled log in db at tool_store_dialled_call_log.java");
else
Log.e("Error: ", "From onReceive at tool_broadcast_receiver_outgoing_call.java. " + _flag);
}
}
catch (Exception e)
{
Log.e("Exception raised", "at onReceive at tool_broadcast_receiver_outgoing_call.java. Exception Message: " + e.getMessage());
e.printStackTrace();
}
}
private void saveCallLogToLocalDB(String _calledNumber)
{
tool_db_functions _db = tool_db_functions.getInstance(new KaarigarFullInfo());
CommonGlobal _clsCommon = CommonGlobal.getInstance();
ContentValues values = new ContentValues();
values.put(_db.COL_LOG_CUST_CALLED_KAARIGAR[0], _calledNumber);
if(_clsCommon._selectedSCode != -1)
values.put(_db.COL_LOG_CUST_CALLED_KAARIGAR[1], 1291);
values.put(_db.COL_LOG_CUST_CALLED_KAARIGAR[3], "00:3:00");
_db.insertRow(_db.TABLE_LOG_CUST_CALLED_KAARIGAR, values);
getDataFromDB(_db);
}