Android Service全屏广播

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

场景


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


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();
   }
}


在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) {
    }
}




相关文章
|
5月前
|
Android开发
Android 11 添加Service服务SELinux问题
Android 11 添加Service服务SELinux问题
233 1
|
5月前
|
Android开发
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
Android基础知识:请解释Service是什么,它与IntentService的区别是什么?
91 0
|
12月前
|
Android开发
Android 全屏适配刘海机型
Android 全屏适配刘海机型
160 0
|
5月前
|
XML Java Android开发
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
Android Studio App开发之服务Service的讲解及实战(包括启动和停止,绑定与解绑,推送服务到前台实现音乐播放器,附源码)
681 0
|
2月前
|
Android开发 开发者
Android中弹框如何设计成全屏的
本文介绍在Android中实现全屏对话框的方法,包括使用`Dialog`和`DialogFragment`两种方式。通过设置对话框无标题、调整布局参数及使用透明背景实现全屏效果。适用于希望提升应用交互体验的开发者。
40 0
|
4月前
|
调度 Android开发
43. 【Android教程】服务:Service
43. 【Android教程】服务:Service
45 2
|
5月前
|
Android开发
Android Service Call /dev/xxx SELinux
Android Service Call /dev/xxx SELinux
93 1
|
2月前
|
编解码 网络协议 Android开发
Android平台GB28181设备接入模块实现后台service按需回传摄像头数据到国标平台侧
我们在做Android平台GB28181设备对接模块的时候,遇到这样的技术需求,开发者希望能以后台服务的形式运行程序,国标平台侧没有视频回传请求的时候,仅保持信令链接,有发起视频回传请求或语音广播时,打开摄像头,并实时回传音视频数据或接收处理国标平台侧发过来的语音广播数据。
|
5月前
|
存储 监控 Java
Android Service之设备存储空间监控 DeviceStorageMonitorService
Android Service之设备存储空间监控 DeviceStorageMonitorService
101 2
|
5月前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
215 3
下一篇
无影云桌面