如何在服务类android中使用游标?

时间:2015-08-10 06:14:26

标签: android android-service android-cursor

在我的应用程序中,我需要在服务类中使用游标。我已经在活动类中使用了游标,但我不知道如何在服务类中使用游标。我尝试过,但它显示错误。任何人请建议我如何使用它。 提前谢谢。

服务类:

     public class BillTracker extends Service {

            @Override
            public void onStart(Intent intent, int startId) {
                super.onStart(intent, startId);
                TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                String operatorName = tm.getSimOperatorName();
                Log.v("TAG_OPERATORNAME", "" + operatorName);

                if(operatorName.equalsIgnoreCase("Airtel") )  {
                    _Airtel_info();
                }

            }

            @Override
            public IBinder onBind(Intent intent) {

                return null;
            }
            public void _Airtel_info() {
                Uri qryinBox = Uri.parse("content://sms/inbox");
                Cursor qryinBoxRes =context.getContentResolver().query(qryinBox, new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"}, null, null, null);
                context.startManagingCursor(qryinBoxRes);
        // startMangingcursor showing error (cannot resolve method)

                String[] columns = new String[]{"address", "person", "date", "body", "type"};

                if (qryinBoxRes.getCount() > 0) {
                    String count = Integer.toString(qryinBoxRes.getCount());
                    while (qryinBoxRes.moveToNext()) {
                        String address = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[0]));
                        long SMSdate = qryinBoxRes.getLong(qryinBoxRes.getColumnIndex(columns[2]));
                        body = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[3]));
                        String name = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[1]));
                        String type = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[4]));
  if (address.contains("+91845435435454") && SMSdate > MonthAgo) {


                }


            }

        }
        }

2 个答案:

答案 0 :(得分:0)

好的是,您无法在服务中使用 startManagingCursor

  

当服务被销毁时,光标会自动关闭

为了更好地理解,请查看以下链接

What's the purpose of startManagingCursor?

您可以做的是您可以收听传入的消息。每当收到消息时,请检查该消息是否来自您需要知道的特定号码,然后执行逻辑。

为了更好地理解,请查看以下链接

Read and display incoming message text android

Android – Listen For Incoming SMS Messages

答案 1 :(得分:0)

您应该尝试单独处理它,这是您的BillTracker.class并使用以下命令编辑该类:

DB db;
static Runnable runnable;

@Override
public void onCreate() {
    super.onCreate();
    db = new DB(getApplicationContext());

runnable = new Runnable() {
        @Override
        public void run() {
            String data = db.getData();
        }
};
}

和DB.class代码如下:

public class DB extends SQLiteOpenHelper {

public DB(Context context ) {
    super(context, DBName, null, DBVersion);
    db = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {

        String CreateTable = "CREATE TABLE IF NOT EXISTS " + Table_Name + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Column1 + " INTEGER, " + Column2+ " VARCHAR(500) )";
    db.execSQL(CreateTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
    this.onCreate(db);
}

public String getData() {
    String query = "SELECT * FROM " + Table_Name;
    Cursor c = db.rawQuery(query, null);

    String yourObject = "";
    if (c.moveToFirst()) {

        while (c.isAfterLast() == false) {

            yourObject= c.getString(c.getColumnIndex(columnName));
            c.moveToNext();
        }
    }
    c.close();

    return yourObject;
}

}