为了操作数据库,我创建了一个扩展SQLiteHelper的类DBAdapter()……该类具有以下构造函数:
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
现在,在包的任何活动中都可以正常工作:
DBAdapter db=new DBAdapter(this);
我正在尝试做的是创建一个新线程,并从该线程访问数据库。
问题是当我尝试做时
在该类中的
db=new DBAdapter(this);
告诉我“错误的上下文” !!!我应该在我的新类的DBAdapter()类中创建一个新的构造函数吗?在DBAdapter()中是否允许两个构造函数?
这是我的班级样子:
public class WorkerRunnable implements Runnable{
public WorkerRunnable(Socket clientSocket){
this.clientSocket=clientSocket;
}
public void run(){
try{
Scanner is =new Scanner(new DataInputStream(this.clientSocket.getInputStream()));
while (is.hasNext()) {
longitude = is.next();
Log.d("Longitudine:",longitude);
latitude=is.next();
Log.d("Latitudine:",latitude);
}
is.close();
clientSocket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
我的构造器应该是什么样?应该在此类的run()方法中调用吗?
public class Server1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server1);
DBAdapter db=new DBAdapter(this);
MultiThreadedServer server = new MultiThreadedServer(6000);
new Thread(server).start();
}
}
如何将Server1活动传递给线程?
这是针对WorkerRunnable的MultiThreadedServer:
公共类MultiThreadedServer实现Runnable {
protected int serverPort=6000;
public static String SERVERIP="10.0.2.15";
protected ServerSocket serverSocket=null;
protected boolean isStopped=false;
protected Thread runningThread=null;
public static int clientconnection = 0;
public MultiThreadedServer(int port){
this.serverPort=port;
}
public void run(){
synchronized(this){
this.runningThread = Thread.currentThread();
}
openServerSocket();
while(!isStopped()){
Socket clientSocket=null;
try{
System.out.println("Serverul asteapta clienti spre conectare");
clientSocket=this.serverSocket.accept();
clientconnection++;
System.out.println("Serverul a acceptat clientul cu numarul:"+clientconnection) ;
//Log.d("Server:","s-a acceptat un nou client");
}
catch(IOException e){
if(isStopped()) {
System.out.println("Server Stopped.") ;
return;
}
throw new RuntimeException(
"Error accepting client connection", e);
}
new Thread(new WorkerRunnable(clientSocket)).start();
}
System.out.println("Server Stopped.") ;
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop(){
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
InetSocketAddress serverAddr =new InetSocketAddress(SERVERIP,serverPort);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
Log.d("Server:","bind-ul a fost realizat!");
} catch (IOException e) {
throw new RuntimeException("Cannot open port 6000", e);
}
}
}
最佳答案
您可以在主Activity中调用DBAdapter db=new DBAdapter(this);
,然后通过其构造函数将其传递给Thread。
或者,您可以将主Activity传递给线程(通过构造函数),然后使用DBAdapter db=new DBAdapter(<activityVar>);
创建DBAdapter
编辑:
因此,您的MultiThreadedServer
的构造函数将类似于此public MultiThreadedServer(int port, DBAdatpter db){
,并且您将从db
传入Server1
。将其扔到MultiThreadedServer
中的全局变量中,并通过更新的构造函数WorkerRunnable
传递给public WorkerRunnable(Socket clientSocket, DBAdapter db){
。
关于android - 为其他线程创建数据库的构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5822363/