method startManagingCursor(Cursor) is undefined for the type Service?
-
28-02-2021 - |
Pergunta
I have created one service. Now I am trying to access database in onstart() of service by creating boject of database class.i want to select records from some table,for that i used cursor.when i write startManagingCursor(cursor object) i occurring error there as method startManagingCursor(cursor object) is undefined for type srvice. now if i want to move cursor or manage it then how can i select records from that table? or is it not necessary to write startManagingCursor(cursor object); in service? if i remove this function then would i get records?here i have attached code:
@Override
public void onStart(Intent intent, int startid)
{
DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(Srvc_Sms_email.this);
dbAdapter1.openDataBase();
String[] sel = {"pid","date","datename"};
Cursor cNames = dbAdapter1.selectRecordsFromDB("datesdatabase",sel,null,null,null,null,null);
startManagingCursor(cNames);
cNames.moveToFirst();
int i1 =0;
while (cNames.isAfterLast() == false)
{
pid.add(cNames.getInt(0));
datelist.add(cNames.getString(1));
namelist.add(cNames.getString(2));
cNames.moveToNext();
}`
The error occurs at startManagingCursor(cNames);
.
Solução
You cannot use startManagingcursor in a service. Managed cursors take care of closing the cursor when the activity is destroyed, and they will be deactivated and requeried as the activities is stopped and restarted. in a service this is not possible. for better understanding verify these answers startManagingCursor() in a service?
and
What's the purpose of startManagingCursor?
without startmanaging cursor you can get the records. but you need to handle the closing of the cursors when you finish. (cNames.close())
and when your getting the adapter try
DBAdapter dbAdapter1 = DBAdapter.getDBAdapterInstance(getApplicationContext());