簡體   English   中英

在sql檢查中兩個輸出都是false

[英]Both output is false in sql check

如果行與收到的字符串相同,我試圖讓我的應用程序通過我的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:我沒有收到任何警告或錯誤。

任何幫助深表感謝!

我知道了! 我正在使用:

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);
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM