请参阅@Yuri从此处发布的代码.如何在一定次数后停止计时器 .如果我想因某些条件而停止它,然后再重新启动它.我该怎么做呢?
private final static int DELAY = 10000;
private final Handler handler = new Handler();
private final Timer timer = new Timer();
private final TimerTask task = new TimerTask() {
private int counter = 0;
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
if(++counter == 4) {
timer.cancel();
}
//do some stuff in my app
//restart the timer again
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer.schedule(task, …
Run Code Online (Sandbox Code Playgroud) 尝试使用a Timer
运行4次,每次间隔10秒.
我试过用循环来阻止它,但它一直在崩溃.尝试过使用schedule()
带有三个参数,但我不知道在哪里实现一个计数器变量.有任何想法吗?
final Handler handler = new Handler();
Timer timer2 = new Timer();
TimerTask testing = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "test",
Toast.LENGTH_SHORT).show();
}
});
}
};
int DELAY = 10000;
for (int i = 0; i != 2 ;i++) {
timer2.schedule(testing, DELAY);
timer2.cancel();
timer2.purge();
}
Run Code Online (Sandbox Code Playgroud) 有没有人用模拟器成功测试过任何新的CoreMotion API?似乎需要M7的那些不起作用.我试过在运行iOS 7的新模拟器上调用这些函数
(BOOL)isActivityAvailable
Run Code Online (Sandbox Code Playgroud)
和
(BOOL)isStepCountingAvailable
Run Code Online (Sandbox Code Playgroud)
但是两个函数调用都返回false.
这是我如何宣布我的名单.当它应该在我的一个列表中返回一对(这是一个字符串,int对)时,it.next()似乎正在返回我的set.有任何想法吗?对于Object类型,方法getFirst()是未定义的.似乎如果我这样做,它就修复了这个问题.
String m =((Pair)it.next()).getFirst();
List <HashSet<Pair>> addresses = new ArrayList <HashSet<Pair>> ();
for (int i = 0; i < 100; i++) {
Iterator it = (addresses.get(i)).iterator();
while (it.hasNext()){
String m = it.next().getFirst()); //getFirst returns a string
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个整数字符串的映射.我想检查地图是否有某个字符串,如果是,请修改它映射到的整数值.
Map <String, Integer> m= new SortedMap <String,Integer>();
Map <String, Integer> m2 = new SortedMap<StringInteger>();
//do some stuff
Iterator <String,Integer> i = m2.iterator();
//add some values into the first map first map
while (i.hasNext()){
String temp = i.next();
int found = m.get(temp);
if ( found != null) {//this is giving me a syntax error , something about how ints
can't be null , do I just compare it to zero
//process value that temp maps to
averages.put(temp, val); //
} …
Run Code Online (Sandbox Code Playgroud)