gpt4 book ai didi

android - 为不同的线程创建数据库的构造函数

转载 作者:行者123 更新时间:2023-11-30 04:41:59 25 4
gpt4 key购买 nike

为了操作数据库,我创建了一个扩展 SQLiteHelper 的类 DBAdapter()...这个类有这个构造函数:

public DBAdapter(Context context) {

super(context, DATABASE_NAME, null, 1);

this.myContext = context;

}

现在在包的任何 Activity 中都可以正常工作:

DBAdapter db=new DBAdapter(this);

我想做的是创建一个新线程并从该线程访问数据库。

问题是当我尝试做的时候

db=new DBAdapter(this); 在该类中它告诉我“错误的上下文”!!!

我应该在 DBAdapter() 类中为我的新类创建一个新的构造函数吗???在 DBAdapter() 中是否允许有两个构造函数??

我的类(class)是这样的:

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()方法中调用吧?Thx!


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 Activity 传递给线程????

这是 WorkerRunnable 类的 MultiThreadedServer:

公共(public)类 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);
}
}

最佳答案

您可以调用 DBAdapter db=new DBAdapter(this);在您的主 Activity 中,然后通过其构造函数将其传递给您的 Thread。
或者您可以将主 Activity 传递给 Thread(通过构造函数),然后使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com