0

In my application i need to use cursor in service class. I already used the cursor in activity class, but i don't know how to use the cursor in service class. I tried that but it shows error. any one please suggest me how to use this. Thanks in advance.

service class:

     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) {


                }


            }

        }
        }
kartheeki j
  • 2,206
  • 5
  • 27
  • 51

2 Answers2

0

Alright the thing is that you cannot use startManagingCursor in your service.

Cursor be automatically closed when the service is destroyed

and for better understanding have a look at below link

What's the purpose of startManagingCursor?

what you can do is that you can listen for incoming messages. Whenever you receive a message then check that if that message is from that specific number that you need to know, and then perform logic.

for better understanding have a look at below link

Read and display incoming message text android

Android – Listen For Incoming SMS Messages

Community
  • 1
  • 1
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
  • thank you. but my aim is every time i need to check (if app closed too) whether any messages are coming with +91845435435454 with this address. so i wrote this in service class. Now how i get this information with out using cursor in service class – kartheeki j Aug 10 '15 at 06:40
  • so no need to use service. i need to use broad cast receiver. am i right? is it possible to write cursor in broad cast receiver. In that link SmsMessage is used rather than cursor? – kartheeki j Aug 10 '15 at 07:02
  • i think you dont need cursor for that. – Moubeen Farooq Khan Aug 10 '15 at 07:20
  • i need smsdate,smsbody, address. with out using cursors i will get these information/ – kartheeki j Aug 10 '15 at 07:24
0

You should try to handle it separately, here is your BillTracker.class and edit the class with:

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

and DB.class code is given below:

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

}
Abdul Rahman Majeed
  • 1,125
  • 2
  • 10
  • 22