I want to delay the toast on selected delay times like (15, 30, 60 seconds and no delay) but it won't work. Here's the code:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(FirstLoad){
FirstLoad = false;
return;
}
Toast.makeText(parent.getContext(), "You chose " +
parent.getItemAtPosition(pos).toString()+ " to delay", Toast.LENGTH_LONG).show();
Message message = new Message();
Bundle bun = new Bundle();
bun.putString("delay", parent.getItemAtPosition(pos).toString());
message.obj = bun;
if (pos == 0) {
handler.sendMessageDelayed(message, 0);
}
else if (pos == 1) {
handler.sendMessageDelayed(message, 15000);
}
else if (pos == 2) {
handler.sendMessageDelayed(message, 30000);
}
else if (pos == 3) {
handler.sendMessageDelayed(message, 60000);
}
//handler.sendMessageDelayed(message, 15000);
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
Help Please.
Declare your handler this way:
Simply, you don't have to use a bundle, but you can call msg.what=THE DELEY TIME. Also, you can call handler.obtainMessage to get a message. See http://developer.android.com/reference/android/os/Handler.html#obtainMessage%28%29 So every time you send a message, it will be handled here, and thus you call show the toast. Sorry that I don't have Eclipse installed on this laptop, so I can not test the code. However, I believe it works.
Try this :
Edit:
How are you fetching the delay? Is it something the user enters in an
EditText
? In that case you could just get the delay like this :and then use that delay amount to post the runnable to the handler like this :
You can remove your entire if-else block in this case.
hope it helps.