我已经尝试了几天,但似乎我无法找到解决方案,我试图从服务中的数据库获取价值,但我的应用程序只是崩溃。但是相同的代码适用于活动,但不适用于后台服务。
这是代码
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();
}
}
}
我没有语法错误。但是当我在没有数据库的情况下调用onstart命令时它工作正常但是当使用数据库时它不起作用应用程序崩溃我也尝试了
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);
}
}
}
答案 0 :(得分:1)
将您的选择查询更改为:
Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = "+id, null);
因为目前您正在传递带有ID的+
(PLUS)where where子句
并使用startService
启动您的服务,而不是手动调用onStartCommand
:
Intent intent=new Intent(cont,Services1.class);
intent.putExtra("from",from);
intent.putExtra("body",body);
cont.startService(intent);
使用from
body
中的onStartCommand
和intent.getStringExtra
值
答案 1 :(得分:1)
您的服务创建中存在拼写错误。变化
public void onCreates() {
与
@Override
public void onCreate() {