android - Toast 消息未在 x 秒的 fragment 中显示?

标签 android android-fragments handler android-toast

我想在我的应用程序中每 2 秒显示一条 toast 消息,该消息处于 fragment 而非 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; 
} 

最佳答案

它对我有用,你可以编写这些代码...

    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 分钟运行一次..

关于android - Toast 消息未在 x 秒的 fragment 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610650/

相关文章:

android - 将显示安全警报的保管箱集成到我的应用程序中

android - 选项卡布局内容未显示在 fragment 布局内

java - 膨胀类 fragment 谷歌 API 时出错

javascript - 如何使 JavaScript 代理的处理程序方法成为异步函数?

java - 与android线程相关的looper、handler等术语是什么?这些类之间有何关系?

android - 以编程方式启用定位模式高精度或省电,无需用户访问设置

android - 如何从可扩展 ListView 中删除组

java - 当我单击标题时,没有显示首选项

vb.net - 如何在 VB.NET 中删除事件处理程序

Android注解REST集头