1

我正在开发一个需要捕获来电号码的应用程序。我希望它通过我的 sqlite 数据库来查找行是否匹配。

服务等级:

public class CallService extends Service {
    private final IBinder mBinder = new MyBinder();
    public String LOG_TAG = "LOG_TAG";
    public int SWITCH = 0;
    Database mDB = new Database(this);
    /*****
     * 0 = get call
     * 1 = forward call
     * 2 = block call
     */

      @SuppressWarnings("deprecation")
    @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          PhoneCallListener phoneListener = new PhoneCallListener();
            TelephonyManager telephonyManager = (TelephonyManager) this
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonyManager.listen(phoneListener,
                    PhoneStateListener.LISTEN_CALL_STATE);

            Notification notification = new Notification(R.drawable.ic_launcher, "Service started", System.currentTimeMillis());

            Intent main = new Intent(this, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, main,  PendingIntent.FLAG_UPDATE_CURRENT);

            notification.setLatestEventInfo(this, "Call Manager", "Call manager's service is running", pendingIntent);
            notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR;

            startForeground(2, notification);
        return Service.START_NOT_STICKY;
      }

      @Override
      public IBinder onBind(Intent arg0) {
        return mBinder;
      }

      private class PhoneCallListener extends PhoneStateListener {

            private boolean isPhoneCalling = false;

            public void onCallStateChanged(int state, String incomingNumber) {

                if (TelephonyManager.CALL_STATE_RINGING == state) {
                    // phone ringing
                    Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
                    mDB.CheckForNumber(incomingNumber);

                    if (mDB.equals(0)) {
                        Log.d("LOG_TAG", "Something is right, i guess");
                    }


                }

                if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                    // active
                    Log.i(LOG_TAG, "OFFHOOK");

                    isPhoneCalling = true;
                }

                if (TelephonyManager.CALL_STATE_IDLE == state) {
                    // run when class initial and phone call ended, need detect flag
                    // from CALL_STATE_OFFHOOK
                    Log.i(LOG_TAG, "IDLE number");

                    if (isPhoneCalling) {

                        Handler handler = new Handler();

                        //Put in delay because call log is not updated immediately when state changed
                        // The dialer takes a little bit of time to write to it 500ms seems to be enough
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                // get start of cursor
                                  Log.i("CallLogDetailsActivity", "Getting Log activity...");
                                    String[] projection = new String[]{Calls.NUMBER};
                                    Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
                                    cur.moveToFirst();
                                    String lastCallnumber = cur.getString(0);

                                    Log.i(LOG_TAG, "Number: " + lastCallnumber + ". Action: ");




                            }
                        },500);

                        isPhoneCalling = false;
                    }

                }
            }
        }

      public class MyBinder extends Binder {
        CallService getService() {
          return CallService.this;
        }
      }

    } 

数据库类:

 public class Database
    {
            public static final String DB_NAME = "callContacts";
            public static final String DB_CONTACTS = "callContactList";
            MyHelper mh;
            Context myCon;
            SQLiteDatabase sdb;
            public Database(Context c) {
                myCon = c;
                mh = new MyHelper(myCon, DB_NAME, null, 1);
            }

            public void open() {
                sdb = mh.getWritableDatabase();
            }

            public class MyHelper extends SQLiteOpenHelper {
                public MyHelper(Context context, String name, CursorFactory factory, int version) {
                    super(context, name, factory, version);
                    // TODO Auto-generated constructor stub
                }

                @Override
                public void onCreate(SQLiteDatabase db){
                    db.execSQL("create table callContactList(_id integer primary key, cname text,caction text,cnumber text);");
                    Log.d("1", "Table Created");
                }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                    // TODO Auto-generated method stub
                }                      
            }

            public void empInsert(ContentValues cv) {
                sdb.insert("callContactList", null, cv);
            }

            public Cursor getEmp() {
                Cursor c = sdb.query("callContactList", null, null, null, null, null, null);
                 return c;
            }

            public void CheckForNumber(String number){
                Cursor c = sdb.rawQuery("select * from callContactList where cname = " + number, null);
                int numRows = c.getCount();
                if(numRows > 0) {
                    //return;
                } else {

                }
            }
    }

日志猫:

10-24 17:41:35.944: E/AndroidRuntime(1494): FATAL EXCEPTION: main
10-24 17:41:35.944: E/AndroidRuntime(1494): java.lang.NullPointerException
10-24 17:41:35.944: E/AndroidRuntime(1494):     at com.spxc.forwardcalls.Database.CheckForNumber(Database.java:59)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at com.spxc.forwardcalls.CallService$PhoneCallListener.onCallStateChanged(CallService.java:67)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:375)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at android.os.Looper.loop(Looper.java:137)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 17:41:35.944: E/AndroidRuntime(1494):     at dalvik.system.NativeStart.main(Native Method)

任何帮助深表感谢!谢谢

4

1 回答 1

1

你需要在你初始化的地方和你拥有的 checnumberopen之前打电话mDB.CheckForNumber(incomingNumber)sdbCursor c = sdb.rawQuery("select * from callContactList where cname = " + number, null)

 public void open() {
            sdb = mh.getWritableDatabase();
        }

编辑:

在数据库中将您的方法更改为以下

public boolean CheckForNumber(String number){
                Cursor c = sdb.rawQuery("select * from callContactList where cname = " + number, null);
                int numRows = c.getCount();
                if(c!=null) {
                    return true;
                } else {
                    reutrn false 
                }
}

然后服役

mDB.open(); 
boolean check= mDB.CheckForNumber(incomingNumber);
if (check) {
  Log.d("LOG_TAG", "Something is right, i guess");
}
于 2013-10-24T18:15:51.267 回答