Mohammed Zeeshan
Mohammed Zeeshan

Reputation: 23

Getting data from database in Service

I have been trying for days but it seems that i cant find the solution i am trying to get value from the database in a services but my app just crashes. But the same code works in the activity but not on a background services.

this is the code

   public class Services1 extends Service {
    Context cont = this;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreates() {
    // TODO Auto-generated method stub

    String go = "running";

    Toast.makeText(cont, go, Toast.LENGTH_LONG).show();
    super.onCreate();
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}


    public void onStartCommand(Intent intent, String Number, String Body ) {
            // TODO Auto-generated method stub


        SmsManager  smsManager = SmsManager.getDefault();

         String phoneNo = Number;
            String sms = Body;

        Data obj = new Data(this);

    //  String dar = obj.Data1("1").toString();


        try{
            SQLiteDatabase db = openOrCreateDatabase("FinalDB", MODE_PRIVATE, null);

            Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 1", null);
            c.moveToFirst();
            String dar = c.getString(c.getColumnIndex("REPLAYS"));

                String api = "Yeap service started";

                String rep = sms.replace(phoneNo+" :", "");
                smsManager.sendTextMessage(phoneNo, null, dar, null, null);

        }catch (Exception e)
        {
            System.out.println("Some Problem Here");
            e.printStackTrace();
        }




        }







}

i have not syntax error. but when i call the onstartcommand without database it works fine but when using database it does not work app crashes i have also tried

        Data obj = new Data(this);

            String dar = obj.Data1("1").toString(); // getting value from my SQLitehelper class it works in activity but not in service.

So my question is that the wrong way of retrieving database in the services or is there a better way.

my sqlitehelper class

public class Data  extends SQLiteOpenHelper {
 static String Database_Name = "FinalDB";
static int Database_Version = 3;
public Data(Context context) {
    super(context, Database_Name, null, Database_Version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

public String Data1(String id)
{


    SQLiteDatabase db = getReadableDatabase();

    Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = +"+id, null);
    c.moveToFirst();
    String repl = c.getString(c.getColumnIndex("REPLAYS"));



    return repl;
}

my broadcaster class

   public class BroadCastRecivers extends BroadcastReceiver{

static String from;
static String body;

@Override
public void onReceive(Context cont, Intent intent) {
    // TODO Auto-generated method 

//  SmsManager  smsManager = SmsManager.getDefault();


        // TODO Auto-generated method s
          //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
           from  =   str  =  msgs[i].getOriginatingAddress();              
                str += " :";
           body =  str += msgs[i].getMessageBody().toString();
                str += "\n";

            }
            //---display the new SMS message---
        //    Toast.makeText(cont, str, Toast.LENGTH_LONG).show();

            Services1 obj = new Services1();

            obj.onStartCommand(intent, from, body);



  }
}


   }

Upvotes: 1

Views: 694

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Change your Select Query as:

Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = "+id, null);

because currently you are passing +(PLUS) with ID in where clause

and Start your service using startService instead of calling onStartCommand manually as:

Intent intent=new Intent(cont,Services1.class);
intent.putExtra("from",from);
intent.putExtra("body",body);
cont.startService(intent);

get from and body value in Services1 onStartCommand using intent.getStringExtra

Upvotes: 1

vault
vault

Reputation: 4087

There is a typo in your service creation. Change

    public void onCreates() {

with

    @Override
    public void onCreate() {

Upvotes: 1

Related Questions