我正在使用app,我设置了一个服务并从套接字获取了一个url。如果设备已解锁,它工作正常但是如果设备被锁定它工作打开浏览器但是网址是空的。这可以告诉你什么是问题? 我的代码在这里。
public class SocketService extends Service {
Socket socket;
private ServerSocket serverSocket;
BufferedReader in = null;
static String url=null;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
new Task().execute();
}
private class Task extends AsyncTask <Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
serverSocket = new ServerSocket(6066);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (true) {
socket = serverSocket.accept();
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
try {
StringBuilder total = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
total.append(line);
}
url = total.toString();
Log.d("NETWORK-RECEIVE","Message!:"+url);
// String url2 = "http://www.cashsource.systelligence.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);
} catch (IOException e) {
e.printStackTrace();
Log.d("NETWORK-RECEIVE", "Something goes wrong: IOException");
}
} finally {
socket.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Service Started", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
帮助我,我缺少什么?
答案 0 :(得分:0)
这是完整的代码吗?你是否使用了一些BoradcastReceiver进行设备锁定/解锁状态?如果是,请尝试在该代码中查找问题。因为服务代码似乎没问题。