gpt4 book ai didi

java - 检查服务中的行

转载 作者:行者123 更新时间:2023-11-30 03:18:39 30 4
gpt4 key购买 nike

我正在开发一个需要接听来电号码的应用程序。我希望它查看我的 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)

非常感谢任何帮助!谢谢

最佳答案

您需要在 mDB.CheckForNumber(incomingNumber) 之前调用 open 因为这是您初始化 sdb 的地方,并且在 checnumber 中您有 Cursor 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");
}

关于java - 检查服务中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572918/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com