Android 在Service修改UI界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/g984160547/article/details/79537601

场景:

产品需要一个全服广播,不管用户在那个界面每隔一段时间都会弹出一个滚动的文字,而且这个不是用推送来做的,后台返回一组数据,然后客户端自己进行处理!如果有更好的方法或者建议都可以跟我说一下,万分感谢,第一次做这个也不知道用什么合适!

Service

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.yudaowawa.ydww.bean.GetDollInvoicesResult;
import com.yudaowawa.ydww.util.OtherUtils;

import java.util.ArrayList;
import weking.lib.utils.LogUtils;
import weking.lib.utils.MyToast;

/**
 * 中奖轮播
 */
public class WinningCarouselService extends Service
{
   private static int time=1*5;
   public static Thread thread;
   public static void setTime(int te) 
   {
      time = te;
   }
   private static  ArrayList<GetDollInvoicesResult.ItemsBean> mDatas;
   private WindowManager.LayoutParams mParams;
   private Context context;
   private int height;
   private LinearLayout ll;
   public WinningCarouselService(){
   }
   @Override
   public IBinder onBind(Intent intent) 
   {
      return null;
   }
   @Override
   public void onCreate() 
   {
      super.onCreate();
      context=this;
      thread = new Thread(new Tick());
      thread.start();
   }
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) 
   {
      return super.onStartCommand(intent, flags, startId);
   }

   @Override
   public void onDestroy() 
   {
      super.onDestroy();
   }
   class Tick implements Runnable  
   {  
       public void run()  
       {  
          try {
              while(time > -1)  
              {
                Thread.sleep(1000);
                time--;
                 if(time<=0)
                 {
                  if(mDatas!=null){
                     int size = mDatas.size();
                     if(size>0)
                     {
                        if(OtherUtils.isAppOnForeground())
                        {
                           int pos = size-1;
                           MyToast.showMyToast(String.valueOf(mDatas.get(pos).getNickname()+"抓中了娃娃"),mDatas.get(pos).getHeadPic());
                           LogUtils.e("WinningCarouselService number"+mDatas.get(pos));
                           mDatas.remove(pos);
                        }
                     }
                  }
                  time=1*5;
               }
               LogUtils.e("WinningCarouselService time"+time);
              }  
         } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e("WinningCarouselService InterruptedException");
         }
       }  
   }
   public static void stop() 
   {  
        if (thread != null) 
        {  
           thread.interrupt();  
           thread = null;  
        }  
    }
   public static void setDatas(GetDollInvoicesResult result) {
      mDatas= (ArrayList<GetDollInvoicesResult.ItemsBean>) result.getItems();
   }
}

Android 在Service修改UI界面  

public static void showMyToast(final String str, final String headUrl) {
    if (!OtherUtils.isAppOnForeground()) {
        return;
    }
    try {
        App.getAppContext().getCurrentActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                LayoutInflater inflater = LayoutInflater.from(App.getAppContext());
                adLayoutView = inflater.inflate(R.layout.layout_toast_ad, null);
                adTextView = (TextView) adLayoutView.findViewById(R.id.tv_toast);
                adIvHeadView = (CircleImageView) adLayoutView.findViewById(R.id.iv_head_icon);
                adTextView.setText(str);
                ImageUtil.setRoundImgView(adIvHeadView, C.BASE_ICON_URL + headUrl, R.mipmap.home_ic_title_right);
                if (adToast != null) {
                    adToast.setView(adLayoutView);
                } else {
                    adToast = new Toast(App.getAppContext());
                    adToast.setView(adLayoutView);
                }
                adToast.setGravity(Gravity./*CENTER*/TOP, 0, DisplayUtil.dip2px(App.getAppContext(), 60));
                adToast.setDuration(Toast.LENGTH_SHORT);
                adToast.show();
            }
        });
    } catch (Exception e) {

    }

}



猜你喜欢

转载自blog.csdn.net/g984160547/article/details/79537601
今日推荐