0

我已经尝试了好几天,但似乎我找不到我试图从服务中的数据库中获取价值的解决方案,但我的应用程序只是崩溃了。但是相同的代码在活动中有效,但在后台服务中无效。

这是代码

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




        }







}

我没有语法错误。但是当我在没有数据库的情况下调用 onstartcommand 它工作正常但是当使用数据库时它不起作用应用程序崩溃我也尝试过

        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.

所以我的问题是在服务中检索数据库的错误方法或者是否有更好的方法。

我的 sqlitehelper 类

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

我的主播课

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



  }
}


   }
4

2 回答 2

1

将您的选择查询更改为:

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

因为目前您+在 where 子句中使用 ID 传递 (PLUS)

并使用以下方式启动您的服务,而不是手动startService调用:onStartCommand

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

在Services1from中使用bodyonStartCommandintent.getStringExtra

于 2013-01-14T11:33:53.563 回答
1

There is a typo in your service creation. Change

    public void onCreates() {

with

    @Override
    public void onCreate() {
于 2013-01-14T11:39:28.223 回答