Я не хотел добавлять ничего, что уже было сказано, поэтому вот некоторые из них, которые я использую, которые не были упомянуты. (Извините, если это слишком долго):
public static class MyExtensions
{
public static bool IsInteger(this string input)
{
int temp;
return int.TryParse(input, out temp);
}
public static bool IsDecimal(this string input)
{
decimal temp;
return decimal.TryParse(input, out temp);
}
public static int ToInteger(this string input, int defaultValue)
{
int temp;
return (int.TryParse(input, out temp)) ? temp : defaultValue;
}
public static decimal ToDecimal(this string input, decimal defaultValue)
{
decimal temp;
return (decimal.TryParse(input, out temp)) ? temp : defaultValue;
}
public static DateTime ToFirstOfTheMonth(this DateTime input)
{
return input.Date.AddDays(-1 * input.Day + 1);
}
// Intentionally returns 0 if the target date is before the input date.
public static int MonthsUntil(this DateTime input, DateTime targetDate)
{
input = input.ToFirstOfTheMonth();
targetDate = targetDate.ToFirstOfTheMonth();
int result = 0;
while (input < targetDate)
{
input = input.AddMonths(1);
result++;
}
return result;
}
// Used for backwards compatibility in a system built before my time.
public static DataTable ToDataTable(this IEnumerable input)
{
// too much code to show here right now...
}
}
open class SubActivity : AppCompatActivity() {
var myRunnable:Runnable
private var myHandler = Handler()
init {
myRunnable = Runnable{
toast("time out")
var intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
fun toast(text: String) {
runOnUiThread {
val toast = Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT)
toast.show()
}
}
override fun onUserInteraction() {
super.onUserInteraction();
myHandler.removeCallbacks(myRunnable)
myHandler.postDelayed(myRunnable, 3000)
}
override fun onPause() {
super.onPause()
myHandler.removeCallbacks(myRunnable)
}
override fun onResume() {
super.onResume()
myHandler.postDelayed(myRunnable, 3000)
}
}
Расширяют Ваше Действие с помощью
YourActivity:SubActivity(){}
для получения до MainActivity, когда Пользователь неактивен после 3 000 миллисекунд на YourActivity
, я использовал предыдущий ответ и преобразовал его в kotlin.