lundi 2 février 2015

How work BroadcastReceiver whith SQLite databases in ANDROID

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 SLD 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 StopDolgBroadcastReceiver 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.stopdolg.StopDolgBroadcastReceiver: 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)


PLEASE HELP! :)


Aucun commentaire:

Enregistrer un commentaire