I have an application which runs in the background and restarts bluetooth programmatically using
...
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.disable();
mBluetoothAdapter.enable();
...
It works fine in Android Marshmallow but not in Nougat. If anyone has come across a solution to this please let me know.
Using Handler you will easily restart bluetooth.
tvCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogConConnect.dismiss();
}
});
tvOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String sec = edtConC.getText().toString();
int second = Integer.parseInt(sec);
int minus = second - 1;
int mSecond = minus*1000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(bluetoothAdapter.isEnabled()){
bluetoothAdapter.disable();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
}
}
},1000);
}
},mSecond);
dialogConConnect.dismiss();
}
});
dialogConConnect.show();
}
});
Related
When I click a resend button first time, the button will disable for 2 seconds.After 2 seconds the button will enable?
I am using this code
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},1000);
}
});
But this code is not working properly.
try this for this purpose you can use Handler(import android.os.Handler;)
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},2000);// set time as per your requirement
}
});
You can use a timer for this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myButton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
myButton.setEnabled(true);
}
});
}
}, 5000);
}
});
Find the solution
In your button click
long mLastClickTime;
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disableButtonTwoSecs();
// Here your implementation
}
});
public static boolean disableButtonTwoSecs() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 2000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
I implemented a program that have a UI. In UI I have 2 button (start and finish). When I clicked start button my service in my app run, and with finish button my service finished.
In my service I connected to xmpp server, when speed of internet is high I don't have any problem, but when speed of internet is low, it takes a long time to connect xmpp server and my UI don't do any thing and crashed. I put connecting to xmpp server in a thread.
my code is:
public class FirstAct extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
btnStart.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
startService(new Intent(act, MainActivity.class));
}});
btnEnd.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
stopService(new Intent(act, MainActivity.class));
}});
}
}
//------------------------
public class MainService extends Service {
#Override
public void onStart(Intent intent, int startId) {
....
toastHandler2.sendEmptyMessage(0);
....
}
private final Handler toastHandler = new Handler() {
#SuppressWarnings({ "null","unused" })
#Override
public void handleMessage(android.os.Message msg){
try{
connectToXmppServer();
}catch(Exception e){
}
}
};
what should I do that, when ConnectToXMPP takes a long time, all of program not crashed???
please help me
I solved my problem with this thread instead of Handler:
void runInBackground() {
new Thread(new Runnable() {
#Override
public void run() {
connectToXmppServer();
}
}).start();
}
can any 1 give me example to perform onClick on button for multiple times when user clicked for 1 time. when i click on button 1 time it should automatically click after delay of 5 seconds for 100 times. how to perform that.
This is my sample code
mUnlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//It should be already ensured that this mSelectedLock is something user is authorized to access
if (mSelectedLock.unlock("RANDOM")) {
mUnlock.setVisibility(View.INVISIBLE);
mUnlock.postDelayed(new Runnable() {
public void run() {
mUnlock.setVisibility(View.VISIBLE);
}
}, 5000);
} else {
Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
}
}
});
#Override
public void onClick(View v) {actionToBeDone();startLoop(0);}
private void startLoop(final int i) {
if(i!=100) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.e("i",""+i);
actionToBeDone();
startLoop(i+1);
}
}, 2000);
}
}
private void actionToBeDone() {
//enter actions you want to be done
Log.e("actionToBeDone","Button Action");
}
int count = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run(){
if(count < 100){
mUnlock.performClick();
}
}
}, 0, 5000);
5000 is the time in miliseconds you can +/- from here.
int xp = 0;
public void practice(View v){
xp = (xp+50);
TextView tv = (TextView)findViewById(R.id.xp);
tv.setText("XP: " + xp);
}
I'm am new at android and I want my Button to be enabled and when you click it disable it for amount of time . How could I do this? Any examples?
Try this
public void practice(View v){
xp = (xp+50);
yourBtn.setEnabled(false);
handler.postDelayed(myRunnable, YOUR_DELAY);
TextView tv = (TextView)findViewById(R.id.xp);
tv.setText("XP: " + xp);
}
Handler code is :
Handler handler=new Handler();
Runnable myRunnable=new Runnable() {
#Override
public void run() {
// call you delay logic or method here
yourBtn.setEnabled(true);
}
}
Thread btnEnabledThread = new Thread() {
#Override
public void run() {
try {
yourBtn.setEnabled(false);
sleep(1000);
} catch (InterruptedException i) {
} finally {
yourBtn.setEnabled(true);
}
}
};
if you want to disable for 1 sec then use this code
btnEnabledThread.start();
I hit my head against the wall, trying to solve this problem for some time. I'm not an experienced Android guy, so its solution might be really simple. That being said, I have the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.adminusers);
super.onCreate(savedInstanceState);
// set user_type for Viewall
mUserType = 0;
lv_adminusersListContent.addFooterView(footerView);
mUsersAdapter = new AdminUsersAdapter(mUsersList, AdminUsers.this);
lv_adminusersListContent.setAdapter(mUsersAdapter);
getUsersList();
}
private void getUsersList() {
if (UsefulFcts.isOnline(AdminUsers.this)) {
if (mThread != null && mThread.isAlive()) {
mThread.interrupt();
}
mThread = new Thread() {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
pDialog = new ProgressDialog(AdminUsers.this);
pDialog.setMessage("Loading.. Please wait!");
pDialog.setCancelable(false);
pDialog.show();
}
});
mUsersList.clear();
mUsersList.addAll(mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad));
mArrayLength = mUsersList.size();
Log.d("arrayLength", String.valueOf(mArrayLength));
Log.d("total_results",
String.valueOf(ParserJSON.total_results));
mHandler.post(new Runnable() {
#Override
public void run() {
// refresh listview
mUsersAdapter.notifyDataSetChanged();
if (mArrayLength < ParserJSON.total_results) {
lv_adminusersListContent
.addFooterView(footerView);
} else if (mArrayLength >= ParserJSON.total_results) {
lv_adminusersListContent
.removeFooterView(footerView);
}
if (0 == mArrayLength) {
Toast.makeText(AdminUsers.this,
"No username found!",
Toast.LENGTH_SHORT).show();
}
pDialog.dismiss();
}
});
}
};
mThread.start();
} else {
Toast.makeText(AdminUsers.this, "Internet connection required!",
Toast.LENGTH_LONG).show();
}
}
#Override
protected void linkUI() {
// begin load_more view
footerView = AdminUsers.this.getLayoutInflater().inflate(
R.layout.loadmore, null);
btn_loadmore = (Button) footerView.findViewById(R.id.btn_loadmore);
// end load_more view
btn_adminusersViewall = (Button) findViewById(R.id.btn_adminusersViewall);
btn_adminusersAdmins = (Button) findViewById(R.id.btn_adminusersAdmins);
btn_adminusersClients = (Button) findViewById(R.id.btn_adminusersClients);
btn_adminusersDevs = (Button) findViewById(R.id.btn_adminusersDevs);
btn_adminAdd = (ImageButton) findViewById(R.id.btn_adminAdd);
lv_adminusersListContent = (ListView) findViewById(R.id.lv_adminusersListContent);
}
#Override
protected void setAction() {
// begin Buttons UnderHeader
btn_adminusersViewall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersViewall,
btn_adminusersClients, btn_adminusersAdmins,
btn_adminusersDevs);
mUserType = 0;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
btn_adminusersClients.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersClients,
btn_adminusersDevs, btn_adminusersAdmins,
btn_adminusersViewall);
mUserType = 1;
mArrayLength = -1;
// view.setVisibility(View.GONE);
getUsersList();
}
});
btn_adminusersDevs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
UsefulFcts.setButtonClicked(btn_adminusersDevs,
btn_adminusersClients, btn_adminusersAdmins,
btn_adminusersViewall);
mUserType = 2;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
btn_adminusersAdmins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUserType = 3;
mArrayLength = -1;
lv_adminusersListContent.removeFooterView(footerView);
getUsersList();
}
});
// end Buttons UnderHeader
btn_loadmore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mThread2 = new Thread() {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
progressBar = new ProgressDialog(
AdminUsers.this);
progressBar
.setMessage("Loading more projects.. ");
progressBar.setCancelable(true);
progressBar.show();
}
});
mUsersList.addAll(mServiceManager.getUsersList(
mUserType, String.valueOf(mArrayLength),
AdminServiceManager.sHowManyToLoad));
mArrayLength = mUsersList.size();
mHandler.post(new Runnable() {
#Override
public void run() {
mUsersAdapter.notifyDataSetChanged();
if (mArrayLength >= ParserJSON.total_results) {
lv_adminusersListContent.removeFooterView(footerView);
}
progressBar.dismiss();
}
});
};
};
mThread2.start();
}
});
Note: I basically have a listview which content is requested from a server depending on which button (looking like a tab) I'm pressing (mUserType = 0/1/2/3). I also use a Load More button (footerView = simple layout with a button).
Everything seems to work just fine, but after "playing" with the buttons a few times (random number!) it crashes, with the following exception thrown:
02-21 11:00:49.252: E/AndroidRuntime(11728): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296461, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
So, I looked back at the code, but couldn't see any "not on UI thread" listview notification.
1. What could the problem be?
2. Is it necessary to use 2 threads (mThread and mThread2), or could I use the same one, as one's work should start when the other ends (or isn't it ?) and should I declare them static or not ?
Also pointing me to any "Good practices" tricks I could use in the future, or "Wrong practices" that I did would be much appreciated!
PS. linkUI() and setAction() are called in super().onCreate().
The error message seems understandable : you are modifying the adapter from mThread:
mUsersList.clear();
mUsersList.addAll(mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad));
You can try something like this:
final List newUserList = mServiceManager.getUsersList(mUserType,
0 + "", AdminServiceManager.sHowManyToLoad);
mArrayLength = newUserList.size();
Log.d("arrayLength", String.valueOf(mArrayLength));
Log.d("total_results",
String.valueOf(ParserJSON.total_results));
mHandler.post(new Runnable() {
#Override
public void run() {
mUsersList.clear();
mUsersList.addAll(newUserList);
// refresh listview
mUsersAdapter.notifyDataSetChanged();