Toast 消息未在 x 秒的片段中显示?
Toast message not displayed at x seconds in fragments?
我想在我的应用程序中每 2 秒显示一条 toast 消息,它不是 activity 的片段。
但是在我的代码中它只显示一次我在下面分享我的代码。
请指导我。提前谢谢你。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_layout_one, container, false);
MapsInitializer.initialize(getActivity());
mMapView = (MapView)rootView.findViewById(R.id.mapView);
mMapView.onCreate(mBundle);
MapsInitializer.initialize(getActivity());
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
/* new DownloadJSON().execute();
setUpMapIfNeeded(rootView); */
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new DownloadJSON().execute();
setUpMapIfNeeded(rootView);
Toast.makeText(getActivity(), "Data Updated!!!! ", Toast.LENGTH_SHORT).show();
Log.e("Data in Log", "");
}
}, 1000);
new DownloadJSON().execute();
setUpMapIfNeeded(rootView);
/*LocationManager locman = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
//locman.requestLocationUpdates(minTime, minDistance, criteria, intent);
locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);*/
return rootView;
}
可能是您的代码 运行 只有一次,这就是 1 秒后的原因。它显示吐司信息。因此,请确保您的代码一次又一次地 运行ning。您是否一次又一次地查看您的日志?
postDelayed 只做一次,最简单的解决方案就是像这样重新发布任务:
final Handler handler = new Handler();
final Runnable repeatingToast = new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "My text!!!",Toast.LENGTH_SHORT).show();
if(your_condition) {
handler.postDelayed(repeatingToast, 1000);
}
}
};
handler.postDelayed(repeatingToast, 1000);
Toast.LENGTH_LONG = 3500; // 3.5 seconds
Toast.LENGTH_SHORT = 2000; // 2 seconds
所以你在第一个 Toast 之前调用下一个 Toast dismissing.Try 线程中的持续时间更长。
它对我有用,你可以这些代码...
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
Log.e("run", "run");
}
});
}
};
timer.schedule(doAsynchronousTask, 60000, 60000);
这些代码是 运行 每 1 分钟后..
我想在我的应用程序中每 2 秒显示一条 toast 消息,它不是 activity 的片段。
但是在我的代码中它只显示一次我在下面分享我的代码。
请指导我。提前谢谢你。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_layout_one, container, false);
MapsInitializer.initialize(getActivity());
mMapView = (MapView)rootView.findViewById(R.id.mapView);
mMapView.onCreate(mBundle);
MapsInitializer.initialize(getActivity());
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
/* new DownloadJSON().execute();
setUpMapIfNeeded(rootView); */
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new DownloadJSON().execute();
setUpMapIfNeeded(rootView);
Toast.makeText(getActivity(), "Data Updated!!!! ", Toast.LENGTH_SHORT).show();
Log.e("Data in Log", "");
}
}, 1000);
new DownloadJSON().execute();
setUpMapIfNeeded(rootView);
/*LocationManager locman = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
//locman.requestLocationUpdates(minTime, minDistance, criteria, intent);
locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);*/
return rootView;
}
可能是您的代码 运行 只有一次,这就是 1 秒后的原因。它显示吐司信息。因此,请确保您的代码一次又一次地 运行ning。您是否一次又一次地查看您的日志?
postDelayed 只做一次,最简单的解决方案就是像这样重新发布任务:
final Handler handler = new Handler();
final Runnable repeatingToast = new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(), "My text!!!",Toast.LENGTH_SHORT).show();
if(your_condition) {
handler.postDelayed(repeatingToast, 1000);
}
}
};
handler.postDelayed(repeatingToast, 1000);
Toast.LENGTH_LONG = 3500; // 3.5 seconds
Toast.LENGTH_SHORT = 2000; // 2 seconds
所以你在第一个 Toast 之前调用下一个 Toast dismissing.Try 线程中的持续时间更长。
它对我有用,你可以这些代码...
final Handler handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
Log.e("run", "run");
}
});
}
};
timer.schedule(doAsynchronousTask, 60000, 60000);
这些代码是 运行 每 1 分钟后..