环境 : android + i-jetty + servlet

MyServlet.java 1 packagecom.synnex.servlet;
2
3 importjava.io.ByteArrayOutputStream;
4 importjava.io.IOException;
5 importjava.io.PrintWriter;
6 importjava.util.List;
7
8 importjavax.servlet.ServletConfig;
9 importjavax.servlet.ServletException;
10 importjavax.servlet.ServletOutputStream;
11 importjavax.servlet.http.HttpServlet;
12 importjavax.servlet.http.HttpServletRequest;
13 importjavax.servlet.http.HttpServletResponse;
14
15 importandroid.content.Context;
16 importandroid.content.Intent;
17 importandroid.content.pm.ApplicationInfo;
18 importandroid.content.pm.PackageInfo;
19 importandroid.content.pm.PackageManager;
20 importandroid.content.pm.PackageManager.NameNotFoundException;
21 importandroid.content.pm.ResolveInfo;
22 importandroid.graphics.Bitmap;
23 importandroid.graphics.Canvas;
24 importandroid.graphics.PixelFormat;
25 importandroid.graphics.drawable.Drawable;
26 importandroid.util.Log;
27
28 public classMyServlet extendsHttpServlet
29{
30 private static final longserialVersionUID=-4258280559794945071L;
31 private finalStringTAG="MyServlet";
32 private finalStringMETHOD="method";
33
34 privateContextcontext;
35
36 public voidinit(ServletConfigconfig) throwsServletException
37{
38 super.init(config);
39context=(Context)config.getServletContext().getAttribute("org.mortbay.ijetty.context");
40Log.v(TAG,"##initandroidcontextsuccess##");
41}
42
43 protected voidservice(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException
44{
45Stringmethod=request.getParameter(METHOD);
46 if("listApp".equals(method))
47{
48response.setContentType("text/html;charset=UTF-8");
49listApp(request,response);
50}
51 else if("appIcon".equals(method))
52{
53appIcon(request,response);
54}
55 else
56{
57response.setContentType("text/html;charset=UTF-8");
58sayHello(request,response);
59}
60}
61
62 private voidappIcon(HttpServletRequestrequest,HttpServletResponseresponse) throwsIOException
63{
64response.setContentType("image/JPEG");
65ServletOutputStreamout=response.getOutputStream();
66PackageManagerpm=context.getPackageManager();
67 try
68{
69Drawableicon=pm.getApplicationIcon(request.getParameter("id"));
70ByteArrayOutputStreambaos= newByteArrayOutputStream();
71drawableToBitmap(icon).compress(Bitmap.CompressFormat.PNG,100,baos);
72 byte[]b=baos.toByteArray();
73out.write(b);
74out.flush();
75}
76 catch(NameNotFoundExceptione)
77{
78Log.e(TAG,"getappiconerror:",e);
79out.write("error".getBytes("UTF-8"));
80}
81response.flushBuffer();
82}
83
84 private voidsayHello(HttpServletRequestrequest,HttpServletResponseresponse) throwsIOException
85{
86PrintWriterwriter=response.getWriter();
87writer.write("HelloWorld!!!");
88writer.flush();
89response.flushBuffer();
90}
91
92 private voidlistApp(HttpServletRequestrequest,HttpServletResponseresponse) throwsIOException
93{
94IntentmainIntent= newIntent(Intent.ACTION_MAIN, null);
95mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
96List<ResolveInfo>apps=context.getPackageManager().queryIntentActivities(mainIntent,0);
97PackageManagerpm=context.getPackageManager();
98
99StringBuilderbuf= newStringBuilder(500);
100buf.append("<table>")
101.append("<tr>")
102.append("<th>AppName</th>")
103.append("<th>AppVersion</th>")
104.append("<th>PackageName</th>")
105.append("<th>AppIcon</th>")
106.append("</tr>");
107StringimgBaseUrl=request.getContextPath()+request.getServletPath()+"?"+METHOD+"=appIcon";
108Log.v(TAG,imgBaseUrl);
109 for(ResolveInfoinfo:apps)
110{
111 try
112{
113PackageInfoappInfo=pm.getPackageInfo(info.activityInfo.packageName,0);
114 if((appInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0)
115{
116buf.append("<tr>")
117.append("<td>").append(appInfo.applicationInfo.loadLabel(pm)).append("</td>")
118.append("<td>").append(appInfo.versionName).append("</td>")
119.append("<td>").append(appInfo.packageName).append("</td>")
120.append("<td><imgwidth='50px'height='50px'src='").append(imgBaseUrl).append("&id=").append(appInfo.packageName).append("'/></td>")
121.append("<td></td>")
122.append("</tr>");
123}
124}
125 catch(NameNotFoundExceptione)
126{
127Log.e(TAG,"getappinfoerror:",e);
128 continue;
129}
130}
131
132buf.append("</table>");
133
134PrintWriterwriter=response.getWriter();
135writer.write(buf.toString());
136writer.flush();
137response.flushBuffer();
138}
139
140 private staticBitmapdrawableToBitmap(Drawabledrawable)
141{
142 intw=drawable.getIntrinsicWidth();
143 inth=drawable.getIntrinsicHeight();
144
145Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;
146Bitmapbitmap=Bitmap.createBitmap(w,h,config);
147Canvascanvas= newCanvas(bitmap);
148drawable.setBounds(0,0,w,h);
149drawable.draw(canvas);
150 returnbitmap;
151}
152}

在浏览器中输入:http://{android_ip}:8080/MyServlet?method=listApp

显示结果:


更多相关文章

  1. [Android] 环境配置之基础开发环境(SDK/Android(安卓)Studio)
  2. Android:自动完成文本框
  3. android SDK安装后设置环境变量
  4. Ubuntu10.04 64(32)位 android开发环境建立
  5. SDK下载地址
  6. ubuntu10.04系统android开发环境配置
  7. Android安装以及Eclipse插件(Google Android) ,在Android(安卓)
  8. android学习日记01-搭配开发环境
  9. [置顶] 搬家、备份后启动Android(安卓)PANIC :Could not open D:

随机推荐

  1. Android NVidia Tegra2平台Camera架构浅
  2. android 实现ImageView按压效果和解决背
  3. Android通过RandomAccessFile 向文件中写
  4. 10、Android中广播接收者-BroadcastRecei
  5. Android中html.fromhtml的使用方法
  6. Android——getSystemService
  7. Android中多图片选择器PhotoPicker库的使
  8. Android中ExpandableListView的用法实例
  9. Android Application详解
  10. android Binder 工作流程