我如何在android小部件中使用新线程

 @Override
public void onUpdate(Context context,AppWidgetManager 
 appWidgetManager,int[] appWidgetIds) {

    onUpdate(context,appWidgetManager,appWidgetIds,null);


} 

public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds,final Intent receiveIntent) {

     RemoteViews rw = new RemoteViews(context.getPackageName(),R.layout.calendar_widget);



    packageManager=context.getPackageManager();

//code for other jobs

if (receiveIntent != null){
             manualUpdate = receiveIntent.getBooleanExtra("manual",false);

            final RemoteViews[] remoteViews= new RemoteViews[]{rw};
            if(manualUpdate){
                remoteViews[0].setViewVisibility(R.id.update_ready_text,View.VISIBLE);//work



                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        remoteViews[0].setViewVisibility(R.id.update_ready_text,View.INVISIBLE);//not work!!
                        System.out.println("handler ran is succes");
                    }
                });

            }
//code for other jobs...
appWidgetManager.updateAppWidget(appWidgetId,rw);
}

//输出结果: I / System.out:onReceive:android.appwidget.action.APPWIDGET_UPDATE

I / System.out:处理程序运行成功。

////////处理程序运行,但是'textview'将不可见。哪种方法合适?.thx寻求帮助!

countmachine 回答:我如何在android小部件中使用新线程

 if(manualUpdate){
                remoteViews[0].setViewVisibility(R.id.update_ready_text,View.VISIBLE);//work



                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        remoteViews[0].setViewVisibility(R.id.update_ready_text,View.INVISIBLE);//
                        appWidgetManager.updateAppWidget(appWidgetIds,rw);
                    }
                });

            }

***我找到了解决方案。我忘记了处理程序中的更新小部件。它有效-:))))

本文链接:https://www.f2er.com/3099522.html

大家都在问