我正在开发一个应用程序,我想在其中运行后台服务以将数据发送到我的 java 服务器。我已经为它编写了代码,它给了我一些错误,但我无法弄清楚自己哪里出了问题。请帮我解决这个问题并提出一些解决方案。
我在 Intent 服务中为它编写了发送函数,其中 m 从 Sq-Lite DB 检索数据并存储在字符串变量中。
但我收到错误
" 每个线程只能创建一个 Looper"
我的服务文件名为 MyService,我还发布了我的日志猫,请建议我一些解决方案,谢谢...!
我的服务.java
package com.ami.wbs;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
Timer timer=new Timer();
int counter=0;
Socket client,clientSocket;
ServerSocket serverSocket;
String ServerIP = "192.168.43.84";
InputStreamReader inputStreamReader;
BufferedReader bufferedReader;
PrintWriter printwriter;
String data,start,end,code,IMEI,username,result,meter_no = null;
String consumer_name,consumer_id,consumer_add,consumer_meter_no,prev_reading,current_reading,path;
DB_Adapter database;
public void setDB(DB_Adapter db){
database = db;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "On create in My service..",Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(timer!=null)
{
timer.cancel();
Toast.makeText(getApplicationContext(), "On destory in My service..",Toast.LENGTH_LONG).show();
}
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Looper.prepare();
Toast.makeText(getApplicationContext(), "On start in My service..",Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "On start command in My service..",Toast.LENGTH_LONG).show();
dosome();
return super.onStartCommand(intent, flags, startId);
}
public void dosome()
{
database = new DB_Adapter(getApplicationContext(), "WBS", null, 1);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(database.count_consumer())
{
Toast.makeText(getApplicationContext(), "Sending", Toast.LENGTH_SHORT).show();
send();
sendimage();
}else
{
Toast.makeText(getApplicationContext(), "DB is empty", Toast.LENGTH_SHORT).show();
//onDestroy();
}
Log.d("MyService",String.valueOf(counter++));
}
},0,5000);
}
public void send()
{
Log.d("MyService","calling snd()...");
start = "@STR";
end = "000$";
code = "05";
username = database.get_username();
consumer_meter_no = database.get_all_consumer_data();
Toast.makeText(getApplicationContext(), "meter no"+consumer_meter_no, Toast.LENGTH_SHORT).show();
current_reading = database.get_current_reading(consumer_meter_no, "CONSUMER");
String username_padd = username;
// padding for username
int fixlen= 10;
int usernamelen= username.length();
if(fixlen>usernamelen)
{
for(int i=0;i<(fixlen-usernamelen);i++)
{
username_padd="*"+username_padd;
}
}
//padding for meter no
String meter_no_padd = consumer_meter_no;
int fixlen1= 20;
int meter_nolen= consumer_meter_no.length();
if(fixlen1>meter_nolen)
{
for(int i=0;i<(fixlen1-meter_nolen);i++)
{
meter_no_padd="*"+meter_no_padd;
}
}
//padding for current reading
String curr_reading_padd = current_reading;
int fixlen2= 6;
int cr_readinglen= current_reading.length();
if(fixlen>cr_readinglen)
{
for(int i=0;i<(fixlen2-cr_readinglen);i++)
{
curr_reading_padd="0"+curr_reading_padd;
}
}
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
IMEI = tm.getDeviceId();//imei no is captured
data = start+code+username_padd+IMEI+meter_no_padd+curr_reading_padd+end;
try {
client=new Socket(ServerIP,2222);
printwriter=new PrintWriter(client.getOutputStream(),true);
printwriter.write(data);
printwriter.flush();
printwriter.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendimage()
{
try {
//image send
client = new Socket(ServerIP,4444);
path = database.get_path(consumer_meter_no);
File file = new File(path);
byte[] mybytearray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
fis.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String receive()
{
try {
//Toast.makeText(getApplicationContext(), "in receive", Toast.LENGTH_SHORT).show();
serverSocket = new ServerSocket(1111);
clientSocket = serverSocket.accept();
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
String msg = bufferedReader.readLine();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
String start1= msg.substring(0,4);
String code1= msg.substring(4, 6);
String result1= msg.substring(6, 16);
String end1= msg.substring(16, 20);
if(start1.equals("@STR") && code1.equals("05") && end1.equals("999$"))
{
/*Toast.makeText(getApplicationContext(), start1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), code1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), result1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), end1, Toast.LENGTH_SHORT).show();*/
result = result1;
remove_padding();
}else
{
/*Toast.makeText(getApplicationContext(), "in else"+start1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "in else"+code1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "in else"+result1, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "in else"+end1, Toast.LENGTH_SHORT).show();*/
result = result1;
remove_padding();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public String remove_padding()
{
//String starusername=usernamepasswordIMEI.substring(0,10);
char star='*';
int count=0;
if(result.charAt(0) == star)
{
for(int i=0;i<result.length();i++)
{
if(result.charAt(i) == star)
{
count++;
}
/*break;*/
}//end for
meter_no=result.substring(count,result.length());
//return meter_no;
}
else
{
System.out.println("user name String doesn't start with *");
meter_no=result;
}
///Toast.makeText(getApplicationContext(), "Ack Received for metr no:-"+meter_no, Toast.LENGTH_LONG).show();
return meter_no;
}
}
原木猫
06-24 09:32:02.557: E/AndroidRuntime(895): FATAL EXCEPTION: main
06-24 09:32:02.557: E/AndroidRuntime(895): java.lang.RuntimeException: Unable to start service com.ami.wbs.MyService@40e75358 with Intent { cmp=com.ami.wbs/.MyService }: java.lang.RuntimeException: Only one Looper may be created per thread
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.os.Looper.loop(Looper.java:137)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 09:32:02.557: E/AndroidRuntime(895): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:32:02.557: E/AndroidRuntime(895): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 09:32:02.557: E/AndroidRuntime(895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 09:32:02.557: E/AndroidRuntime(895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 09:32:02.557: E/AndroidRuntime(895): at dalvik.system.NativeStart.main(Native Method)
06-24 09:32:02.557: E/AndroidRuntime(895): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
06-24 09:32:02.557: E/AndroidRuntime(895): at android.os.Looper.prepare(Looper.java:78)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.os.Looper.prepare(Looper.java:73)
06-24 09:32:02.557: E/AndroidRuntime(895): at com.ami.wbs.MyService.onStart(MyService.java:75)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.Service.onStartCommand(Service.java:450)
06-24 09:32:02.557: E/AndroidRuntime(895): at com.ami.wbs.MyService.onStartCommand(MyService.java:85)
06-24 09:32:02.557: E/AndroidRuntime(895): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
06-24 09:32:02.557: E/AndroidRuntime(895): ... 10 more
06-24 09:32:10.015: E/AndroidRuntime(1075): FATAL EXCEPTION: main
06-24 09:32:10.015: E/AndroidRuntime(1075): java.lang.RuntimeException: Unable to start service com.ami.wbs.MyService@40ce2b78 with Intent { cmp=com.ami.wbs/.MyService }: java.lang.RuntimeException: Only one Looper may be created per thread
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.os.Looper.loop(Looper.java:137)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 09:32:10.015: E/AndroidRuntime(1075): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:32:10.015: E/AndroidRuntime(1075): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 09:32:10.015: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 09:32:10.015: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 09:32:10.015: E/AndroidRuntime(1075): at dalvik.system.NativeStart.main(Native Method)
06-24 09:32:10.015: E/AndroidRuntime(1075): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.os.Looper.prepare(Looper.java:78)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.os.Looper.prepare(Looper.java:73)
06-24 09:32:10.015: E/AndroidRuntime(1075): at com.ami.wbs.MyService.onStart(MyService.java:75)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.Service.onStartCommand(Service.java:450)
06-24 09:32:10.015: E/AndroidRuntime(1075): at com.ami.wbs.MyService.onStartCommand(MyService.java:85)
06-24 09:32:10.015: E/AndroidRuntime(1075): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
06-24 09:32:10.015: E/AndroidRuntime(1075): ... 10 more
DB_适配器
package com.ami.wbs;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DB_Adapter extends SQLiteOpenHelper {
public SQLiteDatabase db;
public static final String KEY_NAME = "USERNAME";
public static final String KEY_PASSWORD = "PASSWORD";
public static final String login = "LOGIN";
public static final String consumer = "CONSUMER";
public static final String consumer_xml = "CONSUMER_XML";
public static final String zone="ZONE_DETAILS";
public static final String subzone="SUBZONE_DETAILS";
public static final String image="IMAGE";
public DB_Adapter(Context context, String name, CursorFactory factory,
int version)
{
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0)
{
// TODO Auto-generated method stub
arg0.execSQL("CREATE TABLE LOGIN(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"USERNAME TEXT NOT NULL," +
"PASSWORD TEXT NOT NULL,"+
"IMEI INTEGER NOT NULL)");
arg0.execSQL("CREATE TABLE IMAGE(METER_NO INTEGER PRIMARY KEY NOT NULL," +
"PATH TEXT NOT NULL)");
arg0.execSQL("CREATE TABLE CONSUMER(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"CONSUMER_ID INTEGER NOT NULL," +
"CONSUMER_NAME TEXT NOT NULL,"+
"CONSUMER_ADDRESS TEXT NOT NULL,"+
"METER_NO INTEGER NOT NULL," +
"PREV_READING INTEGER NOT NULL," +
"CURR_READING INTEGER NOT NULL," +
"IMAGE TEXT NOT NULL)"
);
arg0.execSQL("CREATE TABLE CONSUMER_XML(METER_NO INTEGER PRIMARY KEY NOT NULL," +
"CONSUMER_ID INTEGER NOT NULL," +
"CONSUMER_NAME TEXT NOT NULL,"+
"CONSUMER_ADDRESS TEXT NOT NULL,"+
"PREV_READING INTEGER NOT NULL)");
arg0.execSQL("CREATE TABLE ZONE_DETAILS(ZONE_ID INTEGER PRIMARY KEY NOT NULL," +
"ZONE_NAME TEXT NOT NULL)");
arg0.execSQL("CREATE TABLE SUBZONE_DETAILS(SUBZONE_ID INTEGER PRIMARY KEY NOT NULL," +
"ZONE_ID INTEGER NOT NULL,SUBZONE_NAME TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
// TODO Auto-generated method stub
arg0.execSQL("DROP TABLE IF EXIST LOGIN");
arg0.execSQL("DROP TABLE IF EXIST CONSUMER");
arg0.execSQL("DROP TABLE IF EXIST CONSUMER_XML");
arg0.execSQL("DROP TABLE IF EXIST ZONE_DETAILS");
arg0.execSQL("DROP TABLE IF EXIST SUBZONE_DETAILS");
arg0.execSQL("DROP TABLE IF EXIST IMAGE");
onCreate(arg0);
}
public long signup_detail(String nusername,String npassword,String imei) // call from Sign Up activity
{
ContentValues cv = new ContentValues();
cv.put("USERNAME", nusername);
cv.put("PASSWORD", npassword);
cv.put("IMEI", imei);
db = getWritableDatabase();
return db.insert("LOGIN", null, cv);
}
public boolean check_Authentication(String username, String password) // call from Login Activity
{
Cursor c = getReadableDatabase().rawQuery(
"SELECT USERNAME,PASSWORD FROM " + login + " WHERE "
+ KEY_NAME + "='" + username +"'AND "+KEY_PASSWORD+"='"+password+"'" , null);
if (c.getCount()>0)
return true;
return false;
}
public void sync_delete() // call from Sync menu option(Home Activity)
{
db = getWritableDatabase();
db.delete("ZONE_DETAILS", null, null);
db.delete("SUBZONE_DETAILS", null, null);
}
public void clear() // call from Clear menu option (Home Activity)
{
db = getWritableDatabase();
db.delete("ZONE_DETAILS", null, null);
db.delete("SUBZONE_DETAILS", null, null);
db.delete("CONSUMER", null, null);
db.delete("CONSUMER_XML", null, null);
}
public void forget_password(String username,String password,String imei) // call from Forgot Password Activity
{
String [] args = {username};
db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("PASSWORD", password);
cv.put("IMEI", imei);
db.update("LOGIN", cv, "USERNAME" + "=?", args);
}
public void set_path(String meter_no,String path) // call to save path
{
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("METER_NO", meter_no);
cv.put("PATH", path);
db.insert(image, null, cv);
}
public void addZone(String zone_id,String zone_name) // call from Sync menu option(Home Activity) to add zone details
{
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("ZONE_ID", zone_id);
cv.put("ZONE_NAME", zone_name);
db.insert(zone, null, cv);
}
public void addSubZone(String subzone_id,String zone_id,String subzone_name) // call from Sync menu option(Home Activity) to add subzone details
{
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("SUBZONE_ID", subzone_id);
cv.put("ZONE_ID", zone_id);
cv.put("SUBZONE_NAME", subzone_name);
db.insert(subzone, null, cv);
}
public void addConsumer_details(String meter,String consumer_id,String consumer_name,String consumer_add,String pr)
{ // call from get list button (Home Activity)
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("METER_NO", meter);
cv.put("CONSUMER_ID", consumer_id);
cv.put("CONSUMER_NAME", consumer_name);
cv.put("CONSUMER_ADDRESS", consumer_add);
cv.put("PREV_READING", pr);
db.insert(consumer_xml, null, cv);
}
public void addReading_details(String cid,String cname,String cadd,String mno,String pr,String cr,String path)
{ // call from Reading details Activity on Save button
db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("CONSUMER_ID", cid);
cv.put("CONSUMER_NAME", cname);
cv.put("CONSUMER_ADDRESS", cadd);
cv.put("METER_NO", mno);
cv.put("PREV_READING", pr);
cv.put("CURR_READING", cr);
cv.put("IMAGE", path);
db.insert(consumer, null, cv);
}
public void delete_consumer(String mtrno) // call from Reading details,on Save button
{
String [] args ={mtrno};
db = getWritableDatabase();
db.delete(consumer_xml, "METER_NO" + "=?", args);
}
public boolean count_list_of_consumer() // call from Login activity
{
Cursor c = getReadableDatabase().rawQuery("SELECT METER_NO FROM " + consumer_xml,null);
if (c.getCount()>0)
return true;
return false;
}
public boolean count_consumer() // used in services
{
Cursor c = getReadableDatabase().rawQuery("SELECT METER_NO FROM " + consumer,null);
if (c.getCount()>0)
return true;
return false;
}
public String get_consumer_id(String mtrno,String table) // call to get consumer id
{
String cid = null;
Cursor c = getReadableDatabase().rawQuery("Select * from " + table, null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
cid=c.getString(c.getColumnIndex("CONSUMER_ID"));
}
}while(c.moveToNext());
return cid;
}
public String get_path(String mtrno) // call to get consumer id
{
String path = null;
Cursor c = getReadableDatabase().rawQuery("Select * from IMAGE" , null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
path=c.getString(c.getColumnIndex("PATH"));
return path;
}
}while(c.moveToNext());
return path;
}
public String get_consumer_name(String mtrno,String table) // call to get consumer name
{
String cname=null;
Cursor c = getReadableDatabase().rawQuery("Select * from " + table, null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
cname=c.getString(c.getColumnIndex("CONSUMER_NAME"));
}
}while(c.moveToNext());
return cname;
}
public String get_consumer_address(String mtrno,String table) // call to get consumer address
{
String cadd=null;
Cursor c = getReadableDatabase().rawQuery("Select * from " + table, null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
cadd=c.getString(c.getColumnIndex("CONSUMER_ADDRESS"));
}
}while(c.moveToNext());
return cadd;
}
public String get_previous_reading(String mtrno,String table) // call to get consumer prev reading
{
String cpr=null;
Cursor c = getReadableDatabase().rawQuery("Select * from " + table, null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
cpr=c.getString(c.getColumnIndex("PREV_READING"));
}
}while(c.moveToNext());
return cpr;
}
public String get_current_reading(String mtrno,String table) // call to get consumer current reading
{
String ccr=null;
Cursor c = getReadableDatabase().rawQuery("Select * from " + table, null);
c.moveToFirst();
do
{
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
if(meter_no.equals(mtrno))
{
ccr=c.getString(c.getColumnIndex("CURR_READING"));
}
}while(c.moveToNext());
return ccr;
}
public ArrayList<String> get_meter_id(String table) // call to get list of meter numbers
{
ArrayList<String>c_meter_id=new ArrayList<String>();
Cursor c = getReadableDatabase().rawQuery("Select METER_NO from " + table, null);
c.moveToFirst();
do
{
String s1=c.getString(c.getColumnIndex("METER_NO"));
c_meter_id.add(s1);
}while(c.moveToNext());
return c_meter_id;
}
public ArrayList<String> get_cid(String table) // call to get list of consumer numbers
{
ArrayList<String>c_meter_id=new ArrayList<String>();
Cursor c = getReadableDatabase().rawQuery("Select CONSUMER_ID from " + table, null);
c.moveToFirst();
do
{
String s1=c.getString(c.getColumnIndex("CONSUMER_ID"));
c_meter_id.add(s1);
}while(c.moveToNext());
return c_meter_id;
}
public ArrayList<String> get_zone_name() // call to get list of zone list
{
ArrayList<String>zn=new ArrayList<String>();
Cursor c = getReadableDatabase().rawQuery("Select ZONE_NAME from " + zone, null);
c.moveToFirst();
do
{
String s1=c.getString(c.getColumnIndex("ZONE_NAME"));
zn.add(s1);
}while(c.moveToNext());
return zn;
}
public ArrayList<String> get_subzone_name(String zone_name) // call to get list of subzone
{
SQLiteDatabase db=getReadableDatabase();
String s;
Cursor c1=db.rawQuery("SELECT ZONE_ID FROM "+ zone +" WHERE ZONE_NAME='"+zone_name+"'",null);
c1.moveToFirst();//find first value
do{
s=c1.getString(c1.getColumnIndex("ZONE_ID"));
}
while(c1.moveToNext());//find first pos to next pos
int zone_id=Integer.parseInt(s);
ArrayList<String>szn=new ArrayList<String>();
c1=db.rawQuery("SELECT SUBZONE_NAME FROM "+ subzone +" WHERE ZONE_ID='"+zone_id+"'",null);
c1.moveToFirst();
do
{
String s1=c1.getString(c1.getColumnIndex("SUBZONE_NAME"));
szn.add(s1);
}while(c1.moveToNext());
return szn;
}
public String get_subzone_id(String sub_zone_name) // call to get subzone id
{
SQLiteDatabase db=getReadableDatabase();
String sz_id;
Cursor c2=db.rawQuery("SELECT SUBZONE_ID FROM "+ subzone +" WHERE SUBZONE_NAME='"+sub_zone_name+"'",null);
c2.moveToFirst();
do
{
sz_id=c2.getString(c2.getColumnIndex("SUBZONE_ID"));
}while(c2.moveToNext());
return sz_id;
}
public String get_username() // call to get Username
{
SQLiteDatabase db=getReadableDatabase();
String user_name;
Cursor c2=db.rawQuery("SELECT USERNAME FROM "+ login,null);
c2.moveToFirst();
do
{
user_name=c2.getString(c2.getColumnIndex("USERNAME"));
}while(c2.moveToNext());
return user_name;
}
public String get_all_consumer_data() // used in service
{
Cursor c = getReadableDatabase().rawQuery("Select * from CONSUMER" , null);
c.moveToFirst();
String meter_no=c.getString(c.getColumnIndex("METER_NO"));
return meter_no;
}
}
@ Neil 我使用了你的结构并从 haldelestartIntent 调用了 dosome 并得到以下错误:
06-24 10:34:26.615: E/Trace(830): error opening trace file: No such file or directory (2)
06-24 10:38:39.613: E/AndroidRuntime(830): FATAL EXCEPTION: Timer-1
06-24 10:38:39.613: E/AndroidRuntime(830): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-24 10:38:39.613: E/AndroidRuntime(830): at android.os.Handler.<init>(Handler.java:197)
06-24 10:38:39.613: E/AndroidRuntime(830): at android.os.Handler.<init>(Handler.java:111)
06-24 10:38:39.613: E/AndroidRuntime(830): at android.widget.Toast$TN.<init>(Toast.java:324)
06-24 10:38:39.613: E/AndroidRuntime(830): at android.widget.Toast.<init>(Toast.java:91)
06-24 10:38:39.613: E/AndroidRuntime(830): at android.widget.Toast.makeText(Toast.java:238)
06-24 10:38:39.613: E/AndroidRuntime(830): at com.ami.wbs.MyService$1.run(MyService.java:117)
06-24 10:38:39.613: E/AndroidRuntime(830): at java.util.Timer$TimerImpl.run(Timer.java:284)
06-24 10:38:50.314: E/Trace(924): error opening trace file: No such file or directory (2)
06-24 10:38:50.864: E/AndroidRuntime(924): FATAL EXCEPTION: Timer-0
06-24 10:38:50.864: E/AndroidRuntime(924): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-24 10:38:50.864: E/AndroidRuntime(924): at android.os.Handler.<init>(Handler.java:197)
06-24 10:38:50.864: E/AndroidRuntime(924): at android.os.Handler.<init>(Handler.java:111)
06-24 10:38:50.864: E/AndroidRuntime(924): at android.widget.Toast$TN.<init>(Toast.java:324)
06-24 10:38:50.864: E/AndroidRuntime(924): at android.widget.Toast.<init>(Toast.java:91)
06-24 10:38:50.864: E/AndroidRuntime(924): at android.widget.Toast.makeText(Toast.java:238)
06-24 10:38:50.864: E/AndroidRuntime(924): at com.ami.wbs.MyService$1.run(MyService.java:117)
06-24 10:38:50.864: E/AndroidRuntime(924): at java.util.Timer$TimerImpl.run(Timer.java:284)