Reputation: 1
How work BroadcastReceiver whith SQLite databases in ANDROID Hi! My goal is to write a program to block incoming calls. A list of numbers stored in the database. The program should work on the following schedule: 1) Registers incoming call 2) He takes an incoming phone number 3) Does the SQL query the database for the presence of this number 4) if the number is found in the database - completing a call Below I have pasted the code that is used for this but when you call the program crashes with an error. Please help me! I've tried everything! head boils: (((((
MY COD:
public class BroadcastReceiver extends BroadcastReceiver{
private ListAdapter mAdapter;
private Cursor mCursor;
private static final String[] mContent = new String[] {
MainDBHelper._ID, MainDBHelper.COL_NAME};
public static final String TABLE_NAME = "contact";
public static final String COL_NAME = "first_name";
public MainDBHelper mainDBHelper;
/////////////////// block telephone ///////////////////////
Context context;
private ITelephony telephonyService;
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
SQLiteDatabase db = mainDBHelper.getWritableDatabase(); // Enable my SQLite database
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number"); // get number of incomin call
//--------------------------
mCursor = db.query(TABLE_NAME, new String[] { }, COL_NAME + "=?",
new String[] {phoneNumber}, null, null, null, null);// search request to my SQLite database
String COUNT_IMAGE = Integer.toString(mCursor.getCount());
mCursor.moveToFirst();
if (mCursor.getCount()>0){ // if in database contain of data the .......
String data = mCursor.getString(mCursor.getColumnIndex(COL_NAME));// FOUND NUMBER
Log.d(data, phoneNumber);
if ((phoneNumber != null)) {
setResultData(null);
telephonyService.endCall(); // incoming call has bloced
Log.d("HANG UP", phoneNumber);
}
}
else{}
}catch (Exception e) {
e.printStackTrace();
}
}
}
MY ERROR WHEN INCOMING CALL:
02-02 15:07:43.254: E/AndroidRuntime(22304): FATAL EXCEPTION: main
02-02 15:07:43.254: E/AndroidRuntime(22304): java.lang.RuntimeException: Unable to start receiver com.example.example.BroadcastReceiver: java.lang.NullPointerException
02-02 15:07:43.254: E/AndroidRuntime(22304): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
02-02 15:07:43.254: E/AndroidRuntime(22304): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
MY MainDBHelper code:
public class MainDBHelper extends SQLiteOpenHelper
implements BaseColumns {
public static final String TABLE_NAME = "contact";
public static final String COL_NAME = "first_name";
// public static final String COL_PHONE = "phone";
public static final Uri URI_CONTENT
= Uri.parse("content://com.example.stopdolg.MainDBProvider/contact");
public static final int URI_CODE = 1;
public static final int URI_CODE_ID = 2;
public MainDBHelper(Context context) {
super(context, MainDBProvider.DB_CONTACTS3, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
Cursor c = db.rawQuery(
"SELECT name FROM sqlite_master WHERE type='table' AND name='"
+ TABLE_NAME + "'", null);
try {
if (c.getCount()==0) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_NAME + " TEXT, " + " TEXT);");
ContentValues values = new ContentValues();
}
}
finally {
c.close();
}
}
@Override
public void onUpgrade(
SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
PLEASE HELP! :)
Upvotes: 0
Views: 1506
Reputation: 18923
You need to initialize your class in onReceive()
method before you open(enable) your database:
mainDBHelper = new MainDBHelper(context);
Upvotes: 1