I am a beginner Android developer. I am working on WiFi Direct implementation. My goal is to disconnect one device from another after certain amount of time (Let's say 10 seconds.) after connection occurs. I guess I need to use performClick() and sleep() functions, but I just could not figure out how to use them in my code. Any help would be much appreciated. I put "THINGS TO DO" comment, where I want to do this. Here's the part of my entire code that I have for this:` @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContentView = inflater.inflate(R.layout.device_detail, null);
Button btn_connect = (Button) mContentView.findViewById(R.id.btn_connect);
btn_connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startTime = System.currentTimeMillis();
startTime = System.nanoTime();
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel",
"Connecting to :" + device.deviceAddress, true, true
);
((DeviceActionListener) getActivity()).connect(config);
}
});
Button btn_disconnect = (Button) mContentView.findViewById(R.id.btn_disconnect);
btn_disconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//THINGS TO DO:
//i) PASS 10 SECONDS
//ii) COME HERE AND MAKE THE PROGRAM CLICK HERE BY ITSELF.
((DeviceActionListener) getActivity()).disconnect();
Log.d("ShowWhenDiscon", "It is disconnected!!!! ");
}
});
mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Allow user to pick an image from Gallery or other
// registered apps
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
}
});
return mContentView;
}`
Use Handlers.
handlerTimer.postDelayed(new Runnable(){
public void run() {
// do something here
// You mentioned to click a button, which needs to be run from the UI Thread.
// Use runOnUiThread() for this
}}, 20000);
References: