onResume 不适用于线程计时器
onResume not working with Thread timer
我想要做的是在 onResume 中获取 getIntent();
。所有事情都只发生在 MainActivity
这是我的代码,但它不起作用,什么都不起作用
@Override
protected void onResume() {
Thread timer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = getIntent();
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
if (restaurant_name != null) {
if (restaurant_name.equals("Romys")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89553, 75.82842))
.title("ROMYS"))
.showInfoWindow();
}
} else {
Toast.makeText(MainActivity.this, "It was not", Toast.LENGTH_LONG).show();
}
}
}
};
timer.start();
super.onResume();
}
我尝试了 onResume
onNewInent
但是应用程序崩溃了。
不确定是否仅此而已,但您正在尝试在不是主 UI 线程的线程上执行 Toast
。所以,尝试像这样替换你的吐司:
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
在主 UI 线程上 运行 使用代码:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
}
});
我想要做的是在 onResume 中获取 getIntent();
。所有事情都只发生在 MainActivity
这是我的代码,但它不起作用,什么都不起作用
@Override
protected void onResume() {
Thread timer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = getIntent();
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
if (restaurant_name != null) {
if (restaurant_name.equals("Romys")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89553, 75.82842))
.title("ROMYS"))
.showInfoWindow();
}
} else {
Toast.makeText(MainActivity.this, "It was not", Toast.LENGTH_LONG).show();
}
}
}
};
timer.start();
super.onResume();
}
我尝试了 onResume
onNewInent
但是应用程序崩溃了。
不确定是否仅此而已,但您正在尝试在不是主 UI 线程的线程上执行 Toast
。所以,尝试像这样替换你的吐司:
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
在主 UI 线程上 运行 使用代码:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
}
});