不久前接到一个需求,android设备需要外接客显,客显上需要轮播服务端下发的图片。话不多说,直接开始上代码。
首先,我们要写一个类继承Presentation,代码如下:
public class ImagePresentation extends Presentation {
private Context context;
private ImageView imageView;
private Handler handler =new Handler();
private List images =new ArrayList<>();//这里的用来存放服务端返回的url数组
private int index;//当前轮播图片下标,当轮播到最后第一张再重置为第一张,以此类推
private static final Long TIME =30000L;//轮播间隔时间为30s
public ImagePresentation(Context context, Display display) {
super(context, display);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.presentation_image);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
imageView = (ImageView) findViewById(R.id.imageView);
}
/**
* 设置轮播图片
* @param images
*/
public void setPics(List images) {
if (images ==null || images.isEmpty()) {
return;
}
this.images = images;
}
/**
* 使用图片加载框架Glide根据url加载图片
* @param url
*/
private void showPic(String url) {
Glide.with(context).load(url).into(imageView);
}
/**
* 开始轮播,先调用Presentation的show()方法,然后通过handler控制轮播
*/
public void start() {
try {
show();
}catch (WindowManager.InvalidDisplayException ex) {
}
handler.post(runnable);
}
private Runnablerunnable =new Runnable() {
@Override
public void run() {
if (images ==null ||images.size() ==0) {
return;
}
if (index ==images.size()) {
index =0;
}
showPic(images.get(index));
index++;
handler.postDelayed(runnable,TIME);
}
};
}
然后,我们启动一个service来控制,代码如下:
public class ImageServiceextends Service {
private ImagePresentation imagePresentation;
public static final int TYPE_OPEN_PRESENTATION =0;//打开客显
public static final int TYPE_CLOSE_PRESENTATION =1;//关闭客显
public static final int TYPE_TUPIAN =2;//图片轮播
public static final int TYPE_EXIT_APP =3;//退出应用
public static final StringINTENT_EXTRA_FLAG ="intent_extra_flag";
private Handler handler =new Handler();
private List imageUrl;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
if (imagePresentation ==null) {
initPresentation();
}
int intExtra = intent.getIntExtra(INTENT_EXTRA_FLAG, -1);
if (intExtra ==TYPE_OPEN_PRESENTATION) {
if (imagePresentation !=null)
imagePresentation.start();
}else if (intExtra ==TYPE_CLOSE_PRESENTATION) {
stop();
}else if (intExtra ==TYPE_EXIT_APP) {
stop();
stopSelf();
}else if (intExtra ==TYPE_TUPIAN) {
imageUrl = (List) intent.getSerializableExtra("imageUrl");
if (imagePresentation !=null) {
imagePresentation.setPics(imageUrl);
imagePresentation.start();
}
}
return super.onStartCommand(intent, flags, startId);
}
private void stop() {
if (imagePresentation !=null &&imagePresentation.isShowing()) {
imagePresentation.dismiss();
}
}
/**
* 初始化
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void initPresentation() {
MediaRouter mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
// 获取到被选中的媒体路由,类型为视频路由
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
//获取到路由推荐的presentation display
Display presentationDisplay = route !=null ? route.getPresentationDisplay() :null;
// Dismiss the current presentation if the display has changed.
if (imagePresentation !=null &&imagePresentation.getDisplay() != presentationDisplay) {
imagePresentation.dismiss();
imagePresentation =null;
}
// 显示resentation
if (imagePresentation ==null && presentationDisplay !=null) {
imagePresentation =new ImagePresentation(this, presentationDisplay);
}
}
}
接着,我们再需要启动的地方启动service,代码如下:
//比如你在启动的第一个activity下做初始化的服务调用
Intent intent =new Intent(NewLoginActivity.this, ImageService.class);
intent.putExtra(ImageService.INTENT_EXTRA_FLAG, ImageService.TYPE_OPEN_PRESENTATION);
startService(intent);
//然后在对应的接口解析到url后再调用
Intent intent=new Intent(NewLoginActivity.this, ImageService.class);
intent.putExtra(ImageService.INTENT_EXTRA_FLAG, ImageService.TYPE_TUPIAN);
intent.putExtra("imageUrl", "");//此处应传入服务端返回的url,当然你也可以自己定义一些图片,再ImageService中做接受数据对应的处理
startService(intent);
以上其实主要是根据你传入的不同的常量值在ImageService中做对应的处理,当然你如果需要再展示一些什么其他页面,也可以再新建一个类继承Presentation,然后也在ImageService中相应的控制处理。
欢迎大家互相交流,有需要源码的可留言。