} public static class ParseException extends Exception {
public ParseException(String detailMessage, Throwable throwable) { super(detailMessage, throwable); }
} public static void prepareUserAgent(Context context) { try {
// Read package name and version number from manifest
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); sUserAgent = String.format(context.getString(R.string.template_user_agent),
info.packageName, info.versionName); } catch(NameNotFoundException e) {
Log.e(TAG, \ }
} public static String getPageContent(String title, boolean expandTemplates) throws ApiException, ParseException {
String encodedTitle = Uri.encode(title);
String expandClause = expandTemplates ? WIKTIONARY_EXPAND_TEMPLATES : \ String content = getUrlContent(String.format(WIKTIONARY_PAGE, encodedTitle, expandClause)); try {
JSONObject response = new JSONObject(content);
JSONObject query = response.getJSONObject(\ JSONObject pages = query.getJSONObject(\
JSONObject page = pages.getJSONObject((String) pages.keys().next()); JSONArray revisions = page.getJSONArray(\ JSONObject revision = revisions.getJSONObject(0);
return revision.getString(\ } catch (JSONException e) {
throw new ParseException(\
}
} protected static synchronized String getUrlContent(String url) throws ApiException { if (sUserAgent == null) {
throw new ApiException(\ } HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url);
request.setHeader(\设置客户端标识 try {
HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HTTP_STATUS_OK) {
throw new ApiException(\
status.toString());
} HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent(); //获取HTTP返回的数据
流 ByteArrayOutputStream content = new ByteArrayOutputStream(); int readBytes = 0;
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes); //转化为字节数组流
} return new String(content.toByteArray()); //从字节数组构建String } catch (IOException e) {
throw new ApiException(\
} }
} 有关整个每日维基的widget例子比较简单,主要是帮助大家积累常用代码,了解Android平台 JSON的处理方式,毕竟很多Server还是Java的。 12.Android中使用定时器TimerTask类介绍
在Android平台中需要反复按周期执行方法可以使用Java上自带的TimerTask类,TimerTask相对于Thread来说对于资源消耗的更低,除了使用Android自带的AlarmManager使用Timer定时器是一种更好的解决方法。 我们需要引入import java.util.Timer; 和 import java.util.TimerTask; private Timer mTimer = new Timer(true); private TimerTask mTimerTask; mTimerTask = new TimerTask() {
public void run() {
Log.v(\
} };
mTimer.schedule(mTimerTask, 5000,1000); //在1秒后每5秒执行一次定时器中的方法,比如本文为调用log.v打印输出。 如果想取消可以调用下面方法,取消定时器的执行 while(!mTimerTask.cancel());
mTimer.cancel(); 最后Android123提示大家,如果处理的东西比较耗时还是开个线程比较好,Timer还是会阻塞主线程的执行,更像是一种消息的执行方式。当然比Handler的postDelay等方法更适合处理计划任务。
13.Android应用Icon大小在不同分辨率下定义
对于Android平台来说,不同分辨率下Icon的大小设计有着不同的要求,对于目前主流的HDPI即WVGA级别来说,通常hdpi的应用icon大小为72x72,而标准的mdpi即hvga为48x48,对于目前HTC和Motorola推出的一些QVGA的使用了ldpi,图标为32x32,常见的Android图标大小设计规范如下表所示: Launcher 36 x 36 px 48 x 48 px 72 x 72 px Menu 36 x 36 px 48 x 48 px
72 x 72 px Status Bar 24 x 24 px 32 x 32 px 48 x 48 px Tab 24 x 24 px
32 x 32 px
48 x 48 px Dialog 24 x 24 px 32 x 32 px
48 x 48 px List View 24 x 24 px 32 x 32 px 48
x
48
px
对于
android
界面设计的安全色,如下表
而对
于系统自带默认程序的图标,下面为png的透明格式,直接鼠标右键另存为即可
看看sdk文档上的关于界面图标的详细说明。 14.Android控件美化Shape你会用吗?
如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了
xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、stroke、corners等。 我们就以目前系统的Button的selector为例说下: android:startColor=\ android:endColor=\ android:angle=\ android:width=\ android:color=\ android:radius=\ android:left=\ android:top=\ android:right=\ android:bottom=\
xmlns:android=\http://schemas.android.com/apk/res/android\
android:startColor=\ android:endColor=\ android:angle=\ android:width=\ android:color=\ android:radius=\ android:left=\ android:top=\ android:right=\ android:bottom=\ android:startColor=\ android:endColor=\ android:angle=\ android:width=\ android:color=\ android:radius=\ android:left=\ android:top=\ android:right=\ android:bottom=\ android:startColor=\ android:endColor=\ android:angle=\ android:width=\ android:color=\ android:radius=\ android:left=\ android:top=\ android:right=\ android:bottom=\ 注意Android123提示大家,以上几个item的区别主要是体现在state_pressed按下或state_focused获得焦点时,当当来判断显示什么类型,而没有state_xxx属性的item可以看作是常规状态下。 15. Android开发者应该保持以下特质 Android123推荐新手应该遵循 1. 深读SDK文档 2. 深读SDK的APIDemo和Samples 3. 掌握GIT开源代码 4. 多了解Android开源项目,学习别人的手法写程序。 16. Android数组排序常见方法 Android的数组排序方式基本上使用了Sun原生的Java API实现,常用的有Comparator接口实现compare方法和Comparable接口的compareTo方法,我们对于一个数组列表比如ArrayList可以通过这两个接口进行排序和比较,这里Android123给大家一个例子 private final Comparator cwjComparator = new Comparator() { private final Collator collator = Collator.getInstance(); public final int compare(Object a, Object b) {