我正在尝试在 Windows 终端应用程序上获得非阻塞 I/O(仅限 Windows,抱歉!)。
如果我想要一个短的输入时间,用户可以在其中按下按钮,但如果他不输入就会停止并且程序会继续,该怎么办?
例如:
一个从 1 计数到用户按下某个键时停止的计时器:我应该有一个 while 循环,但如果我执行 getch 或 getchar 函数,它会停止程序,对吧?
我知道我可以使用 kbhit(); ,但对于“程序”,我试图让我需要知道输入,而不仅仅是如果有输入!是否有任何简单的函数可以让我像键盘缓冲区中的最后一个键一样读取?
我没有尝试实现本地 VpnService 来让我的应用程序执行一些任务,但我对如何停止它启动时有点困惑。VpnService 类和客户端活动基于此 repo:https : //github.com/hexene/LocalVPN
调用者活动基本上是这样的:
public class MainActivity extends AppCompatActivity {
private static final int VPN_REQUEST_CODE = 0x0F;
private boolean waitingForVPNStart;
private BroadcastReceiver vpnStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (LocalVPNService.BROADCAST_VPN_STATE.equals(intent.getAction()))
if (intent.getBooleanExtra("running", false))
waitingForVPNStart = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button vpnButton = (Button)findViewById(R.id.vpn);
vpnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVPN();
}
});
final Button vpnStopButton = (Button)findViewById(R.id.stopVpnButton); …Run Code Online (Sandbox Code Playgroud)