当前位置: 编程技术>移动开发
本页文章导读:
▪监控网络变化的广播接收者 重复调用 如何解决 监控网络变化的广播接收者 重复调用 怎么解决
当没有信号时 会掉线, 我想实现恢复网络连接(wifi或者移动数据)时自动重连。 可是当我关闭wif再打开i时, 这个网络已连接 log打印了两次.........
▪ 输入法 小结-3 输入法 总结-3
public class InputApplication extends Application{ private static final int SERVER_PORT = 9999; private String clientAsk; private Handler handler; private Socket socket = null; .........
▪ 输入法 小结-4 输入法 总结-4
public class InputActivity extends InputMethodService{ private InputApplication application; private static final String TAG = "INPUT"; private InputConnection connection; private Socket socket = null; .........
[1]监控网络变化的广播接收者 重复调用 如何解决
来源: 互联网 发布时间: 2014-02-18
监控网络变化的广播接收者 重复调用 怎么解决
当没有信号时 会掉线, 我想实现恢复网络连接(wifi或者移动数据)时自动重连。 可是当我关闭wif再打开i时, 这个网络已连接 log打印了两次。。
造成两次启动联网线程。 有什么解决办法?
当没有信号时 会掉线, 我想实现恢复网络连接(wifi或者移动数据)时自动重连。 可是当我关闭wif再打开i时, 这个网络已连接 log打印了两次。。
造成两次启动联网线程。 有什么解决办法?
IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(receiver, filter);
public class ConnectivityReceiver extends BroadcastReceiver { private static final String TAG = "ConnectivityReceiver"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo != null) { Log.i(TAG,"Active Network Type = " + activeNetworkInfo.getTypeName()); Log.i(TAG,"Active Network Type = " + activeNetworkInfo.getType()); Log.i(TAG,"Active Network State = " + activeNetworkInfo.getState()); if (activeNetworkInfo.isConnected() ) { Log.i(TAG,"网络已连接"); // TODO 登录账户 } } else { Log.i(TAG,"网络不可用"); } }
[2] 输入法 小结-3
来源: 互联网 发布时间: 2014-02-18
输入法 总结-3
public class InputApplication extends Application
{
private static final int SERVER_PORT = 9999;
private String clientAsk;
private Handler handler;
private Socket socket = null;
public String getClientAsk()
{
return clientAsk;
}
public void setClientAsk(String clientAsk)
{
this.clientAsk = clientAsk;
}
public void startThread()
{
new ServerThread().start();
}
private void createMessage(int code,String data)
{
Bundle bundle = new Bundle();
bundle.putString("ask", data);
Message msg = new Message();
msg.what = code;
msg.setData(bundle);
handler.dispatchMessage(msg);
}
class ServerThread extends Thread
{
@Override
public void run()
{
DataInputStream dis = null;
try
{
String ask = null;
ServerSocket ss = new ServerSocket(SERVER_PORT);
while (true)
{
// 服务器接收到客户端的数据后,创建与此客户端对话的Socket
Socket socket = ss.accept();
// 用于接收客户端发来的数据的输入流
dis = new DataInputStream(socket.getInputStream());
ask = dis.readUTF();
Log.v("INPUT", "-----------"+ask);
if (handler != null)
{
createMessage(10, ask);
}
System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
}
// socket = new Socket("10.74.143.45",SERVER_PORT);
// //获取输出流,用于客户端向服务器端发送数据
// DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
// //获取输入流,用于接收服务器端发送来的数据
// dis = new DataInputStream(socket.getInputStream());
// //客户端向服务器端发送数据
// dos.writeUTF("我是客户端,请求连接!");
// //打印出从服务器端接收到的数据
// String ask = dis.readUTF();
// Log.v("INPUT", "-----------"+ask);
}
catch (IOException e)
{
}
}
}
}
public class InputApplication extends Application
{
private static final int SERVER_PORT = 9999;
private String clientAsk;
private Handler handler;
private Socket socket = null;
public String getClientAsk()
{
return clientAsk;
}
public void setClientAsk(String clientAsk)
{
this.clientAsk = clientAsk;
}
public void startThread()
{
new ServerThread().start();
}
private void createMessage(int code,String data)
{
Bundle bundle = new Bundle();
bundle.putString("ask", data);
Message msg = new Message();
msg.what = code;
msg.setData(bundle);
handler.dispatchMessage(msg);
}
class ServerThread extends Thread
{
@Override
public void run()
{
DataInputStream dis = null;
try
{
String ask = null;
ServerSocket ss = new ServerSocket(SERVER_PORT);
while (true)
{
// 服务器接收到客户端的数据后,创建与此客户端对话的Socket
Socket socket = ss.accept();
// 用于接收客户端发来的数据的输入流
dis = new DataInputStream(socket.getInputStream());
ask = dis.readUTF();
Log.v("INPUT", "-----------"+ask);
if (handler != null)
{
createMessage(10, ask);
}
System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
}
// socket = new Socket("10.74.143.45",SERVER_PORT);
// //获取输出流,用于客户端向服务器端发送数据
// DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
// //获取输入流,用于接收服务器端发送来的数据
// dis = new DataInputStream(socket.getInputStream());
// //客户端向服务器端发送数据
// dos.writeUTF("我是客户端,请求连接!");
// //打印出从服务器端接收到的数据
// String ask = dis.readUTF();
// Log.v("INPUT", "-----------"+ask);
}
catch (IOException e)
{
}
}
}
}
[3] 输入法 小结-4
来源: 互联网 发布时间: 2014-02-18
输入法 总结-4
public class InputActivity extends InputMethodService
{
private InputApplication application;
private static final String TAG = "INPUT";
private InputConnection connection;
private Socket socket = null;
@Override
public void onCreate()
{
if (connection == null)
{
connection = getCurrentInputConnection();
}
application = (InputApplication) getApplication();
super.onCreate();
}
@Override
public View onCreateInputView()
{
return super.onCreateInputView();
}
@Override
public View onCreateCandidatesView()
{
// Log.v(TAG, "InputMethodService.onCreateCandidatesView()被调用");
return super.onCreateCandidatesView();
}
@Override
public void onStartInput(EditorInfo editorInfo, boolean restarting)
{
super.onStartInput(editorInfo, restarting);
}
@Override
public void onStartInputView(EditorInfo info, boolean restarting)
{
Myhandler handler = new Myhandler();
application.setHandler(handler);
Log.v(TAG, "InputMethodService.onStartInputView()被调用");
super.onStartInputView(info, restarting);
}
@Override
public void onFinishInput()
{
super.onFinishInput();
}
@Override
public void onFinishInputView(boolean finishingInput)
{
try
{
if (finishingInput && socket != null)
{
//获取输出流,用于客户端向服务器端发送数据
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//客户端向服务器端发送数据
dos.writeUTF("我是客户端,请求连接!");
}
}
catch (Exception e)
{
}
super.onFinishInputView(finishingInput);
}
@Override
public void setCandidatesViewShown(boolean shown)
{
super.setCandidatesViewShown(shown);
}
@Override
public void onBindInput()
{
super.onBindInput();
}
class Myhandler extends Handler
{
@Override
public void dispatchMessage(Message msg)
{
switch (msg.what)
{
case 10:
String result = (String) msg.getData().get("ask");
Log.v("INPUT", result);
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
input.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
connection = getCurrentInputConnection();
connection.commitText(result, 1);
break;
default:
break;
}
super.dispatchMessage(msg);
}
}
}
2:client通道
Socket socket = new Socket("10.0.0.1",9999);
//获取输出流,用于客户端向服务器端发送数据
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//客户端向服务器端发送数据
Scanner scanner = new Scanner(System.in);
String ask = scanner.nextLine();
dos.writeUTF(ask);
socket.close();
public class InputActivity extends InputMethodService
{
private InputApplication application;
private static final String TAG = "INPUT";
private InputConnection connection;
private Socket socket = null;
@Override
public void onCreate()
{
if (connection == null)
{
connection = getCurrentInputConnection();
}
application = (InputApplication) getApplication();
super.onCreate();
}
@Override
public View onCreateInputView()
{
return super.onCreateInputView();
}
@Override
public View onCreateCandidatesView()
{
// Log.v(TAG, "InputMethodService.onCreateCandidatesView()被调用");
return super.onCreateCandidatesView();
}
@Override
public void onStartInput(EditorInfo editorInfo, boolean restarting)
{
super.onStartInput(editorInfo, restarting);
}
@Override
public void onStartInputView(EditorInfo info, boolean restarting)
{
Myhandler handler = new Myhandler();
application.setHandler(handler);
Log.v(TAG, "InputMethodService.onStartInputView()被调用");
super.onStartInputView(info, restarting);
}
@Override
public void onFinishInput()
{
super.onFinishInput();
}
@Override
public void onFinishInputView(boolean finishingInput)
{
try
{
if (finishingInput && socket != null)
{
//获取输出流,用于客户端向服务器端发送数据
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//客户端向服务器端发送数据
dos.writeUTF("我是客户端,请求连接!");
}
}
catch (Exception e)
{
}
super.onFinishInputView(finishingInput);
}
@Override
public void setCandidatesViewShown(boolean shown)
{
super.setCandidatesViewShown(shown);
}
@Override
public void onBindInput()
{
super.onBindInput();
}
class Myhandler extends Handler
{
@Override
public void dispatchMessage(Message msg)
{
switch (msg.what)
{
case 10:
String result = (String) msg.getData().get("ask");
Log.v("INPUT", result);
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
input.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
connection = getCurrentInputConnection();
connection.commitText(result, 1);
break;
default:
break;
}
super.dispatchMessage(msg);
}
}
}
2:client通道
Socket socket = new Socket("10.0.0.1",9999);
//获取输出流,用于客户端向服务器端发送数据
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
//客户端向服务器端发送数据
Scanner scanner = new Scanner(System.in);
String ask = scanner.nextLine();
dos.writeUTF(ask);
socket.close();
最新技术文章: