Android:可以通过微调器延迟消息
用户在旋转器中选择延迟多长时间(例如,15、30、60 秒或无延迟),然后当他单击发送按钮时,延迟将在消息真正发送之前生效,然后在消息真正发送时生效。发送时会有一个 toast 通知用户他的消息已发送。
问题是,如何在微调器上实现延迟?它会出现在 OnitemSelected 中吗?然后在那里拖延?或者什么?
这是代码:
public class KAHTextApp extends Activity {
Button btnRecipient;
Button btnSend;
EditText editTextRecipient;
EditText editTextNewMessage;
Spinner spinnerTimeDelay;
private static final int CONTACT_PICKER_RESULT = 1001;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compose_message);
btnRecipient = (Button) findViewById(R.id.button_recipient_picker);
btnSend = (Button) findViewById(R.id.button_send);
editTextRecipient = (EditText) findViewById(R.id.editText_recipient);
editTextNewMessage = (EditText) findViewById(R.id.editText_new_message);
spinnerTimeDelay = (Spinner) findViewById(R.id.spinner_delay);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.delay_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTimeDelay.setAdapter(adapter);
spinnerTimeDelay.setOnItemSelectedListener(new TimeDelay());
btnRecipient.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONTACT_PICKER_RESULT):
if (resultCode == Activity.RESULT_OK) {
StringBuilder sb = new StringBuilder();
Uri contactData = data.getData();
Cursor contactsCursor = managedQuery(contactData,
null, null, null, null);
if (contactsCursor.moveToFirst()) {
String id = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhoneNumber = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
sb.append(name);
if (Integer.parseInt(hasPhoneNumber) > 0) {
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(phoneNumber);
}
} else {
sb.append("This contact doesn't have a phone number");
}
editTextRecipient.setText(sb.toString());
}
}
break;
}
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String phoneNo = editTextRecipient.getText().toString();
String message = editTextNewMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
Toast.makeText(getBaseContext(),
"Message sent!",
Toast.LENGTH_SHORT).show();
/*sendSMS(phoneNo, message); */
else
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}
这是旋转器的代码:
public class TimeDelay implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The delay is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
The user chooses among the 4 choices on how long the delay is in a spinner (e.g, 15, 30, 60 seconds or no delay) then when he clicks on the send button the delay will take effect before the message really sends then when it sends there'll be a toast to notify the user that his message is sent.
The question is, how do I implement the delay on the spinner? Is it going to be in OnitemSelected? then delay it there? or whatsoever?
Here's the code:
public class KAHTextApp extends Activity {
Button btnRecipient;
Button btnSend;
EditText editTextRecipient;
EditText editTextNewMessage;
Spinner spinnerTimeDelay;
private static final int CONTACT_PICKER_RESULT = 1001;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compose_message);
btnRecipient = (Button) findViewById(R.id.button_recipient_picker);
btnSend = (Button) findViewById(R.id.button_send);
editTextRecipient = (EditText) findViewById(R.id.editText_recipient);
editTextNewMessage = (EditText) findViewById(R.id.editText_new_message);
spinnerTimeDelay = (Spinner) findViewById(R.id.spinner_delay);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.delay_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTimeDelay.setAdapter(adapter);
spinnerTimeDelay.setOnItemSelectedListener(new TimeDelay());
btnRecipient.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONTACT_PICKER_RESULT):
if (resultCode == Activity.RESULT_OK) {
StringBuilder sb = new StringBuilder();
Uri contactData = data.getData();
Cursor contactsCursor = managedQuery(contactData,
null, null, null, null);
if (contactsCursor.moveToFirst()) {
String id = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhoneNumber = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
sb.append(name);
if (Integer.parseInt(hasPhoneNumber) > 0) {
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append(phoneNumber);
}
} else {
sb.append("This contact doesn't have a phone number");
}
editTextRecipient.setText(sb.toString());
}
}
break;
}
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String phoneNo = editTextRecipient.getText().toString();
String message = editTextNewMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
Toast.makeText(getBaseContext(),
"Message sent!",
Toast.LENGTH_SHORT).show();
/*sendSMS(phoneNo, message); */
else
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
});
}
and here's the code for the spinner:
public class TimeDelay implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The delay is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用计时器(处理程序)进行延迟,意味着在其中编写用于发送和烘烤消息的代码,并根据延迟时间(从微调器中选择)调用该函数,只需从微调器中选择一个值即可。通过使用该值调用函数(延迟)。
这是功能
微调器
use the timer's (Handlers) for delay, means write the code for sending and toast message in that and call that function depending on the delay time ( selected from spinner), just select a value from the spinner. by using this value call the function (delay).
this is the function
spinner