77范文网 - 专业文章范例文档资料分享平台

android单实例运行方法(2)

来源:网络收集 时间:2019-03-22 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

} 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=\

对于上面,这条shape的定义,分别为渐变,在gradient中startColor属性为开始的颜色,endColor为渐变结束的颜色,下面的angle是角度。接下来是stroke可以理解为边缘,corners为拐角这里radius属性为半径,最后是相对位置属性padding。 对于一个Button完整的定义可以为

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

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库android单实例运行方法(2)在线全文阅读。

android单实例运行方法(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/537780.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: