在sql check中两个输出都是false

时间:2013-10-24 21:22:13

标签: java android sql sqlite boolean

如果一行与收到的字符串相同,我试图让我的应用程序查看我的sqlite数据库。但我无法弄清楚如何做到这一点,如果它存在于数据库中,或者它不是......那么我给的代码就是假的......

我的CallService,寻找匹配:

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.open(); 
                    boolean check= mDB.CheckNumber(incomingNumber);
                    if (check) {
                      Log.d("LOG_TAG", "" + check);
                    }else {
                        Log.d("LOG_TAG", "" + check);
                    }

                }

                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 boolean CheckForNumber(String number){
            Cursor c = sdb.rawQuery("select * from callContactList where cname = " + number, null);
            //int numRows = c.getCount();
            int value= c.getColumnIndex("cname");
            if (c != null) {
            c.moveToFirst();
            Log.i("..........",""+c.getString(1));
            return true;
            } else {
                return false;
            }

        }*/

        @SuppressWarnings({ "finally", "resource" })
        public boolean CheckNumber(String number) throws SQLException {
            int count = -1;
            Cursor c = null; 
            try {
               String query = "select * from callContactList where cname = " + number;
               c = sdb.rawQuery(query, new String[] {number});
               if (c.moveToFirst()) {
                  count = c.getInt(0);
               }
               return count > 0;
            }
            finally {
               if (c != null) {
                  Log.d("LOG_TAG", "True");
                  c.close();                  
                  return true;
               }else{
                   Log.d("LOG_TAG", "False");
                   return false;
               }
            }
        }
}

logcat的: 我没有收到任何警告或错误。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我明白了! 我正在使用:

public boolean verification(String _username) {
    Cursor c = dataBase.rawQuery("SELECT 1 FROM "+TABLE_NAME+" WHERE "+KEY_USERNAME+"=?", new String[] {_username});
    boolean exists = c.moveToFirst();
    c.close();
    return exists;
}

然后在服务类中:

mDB.open(); 
                    boolean check= mDB.verification(incomingNumber);
                    if (check) {
                      Log.d("LOG_TAG", "" + check);
                    }else {
                        Log.d("LOG_TAG", "" + check);
                    }