现在的位置: 首页 > 综合 > 正文

android开发技巧精髓三

2013年02月20日 ⁄ 综合 ⁄ 共 7880字 ⁄ 字号 评论关闭

10. Android Theme和Styles内部定义解析

昨天我们讲到的有关在AndroidManifest.xml中定义Activity的theme方法来实现无标题的方法,在使用xml让你的Activity无标题方法 一文中讲到的,很多网友不明白为什么这样做,其实在Android123以前的文章中多次提到了styles样式定义方法,今天Android开发网再次 把一些网友回顾了解下android样式的内部定义。在一个工程的res/values/theme.xml中我们可以方便的定义自己的风格主题,比如下
面的cwjTheme中我们使用了基于android内部的白色调的背景Theme.Light,设置windowsNoTitle为true代表没有标 题,背景颜色我们使用了android内部定义的透明,同时设置listView控件的样式为cwjListView,xml样式代码如下:

  <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="cwjTheme" parent="android:Theme.Light">
   <itemname="android:windowNoTitle">true</item>
   <itemname="android:windowBackground">@android:color/transparent</item>
   <item name="android:listViewStyle">@style/cwjListView</item>
</style> 

有关ListView控件我们自定义的风格就是修改下系统listview这个控件的每行分隔符样式,这里我们在工程下res/drawable文件夹下放一个图片名为list_selector图片,这样我们的cwjListView的代码可以这样写

  <style name="cwjListView"parent="@android:style/Widget.ListView">
     <item name="android:listSelector">@drawable/list_selector</item>
   </style>
</resources>

  通过定义style可以设置更多,比如让cwjListView的字体颜色就加入textAppearance属性,比如 <itemname="textAppearance">@android:style/TextAppearance</item> 等等。

11.Android JSON解析示例代码

来 自Google官方的有关Android平台的JSON解析示例,如果远程服务器使用了json而不是xml的数据提供,在Android平台上已经内置 的org.json包可以很方便的实现手机客户端的解析处理。下面Android123一起分析下这个例子,帮助Android开发者需要有关 HTTP通讯、正则表达式、JSON解析、appWidget开发的一些知识。

public class WordWidget extends AppWidgetProvider { //appWidget
    @Override
    public void onUpdate(Context context, AppWidgetManagerappWidgetManager,
            int[]appWidgetIds) {
        context.startService(newIntent(context, UpdateService.class)); //避免ANR,所以Widget中开了个服务
    }

    public static class UpdateServiceextends Service {
        @Override
        public void onStart(Intent intent,int startId) {
            // Build thewidget update for today
            RemoteViewsupdateViews = buildUpdate(this);

           ComponentName thisWidget = new ComponentName(this, WordWidget.class);
            AppWidgetManagermanager = AppWidgetManager.getInstance(this);
           manager.updateAppWidget(thisWidget, updateViews);
        }

        publicRemoteViews buildUpdate(Context context) {
            // Pick outmonth names from resources
            Resourcesres = context.getResources();
            String[]monthNames = res.getStringArray(R.array.month_names);

            Time today = new Time();
           today.setToNow();

           String pageName = res.getString(R.string.template_wotd_title,
                   monthNames[today.month], today.monthDay);
            RemoteViewsupdateViews = null;
            StringpageContent = "";

           try {
               SimpleWikiHelper.prepareUserAgent(context);
               pageContent = SimpleWikiHelper.getPageContent(pageName, false);
            } catch(ApiException e) {
               Log.e("WordWidget", "Couldn't contact API", e);
            } catch(ParseException e) {
               Log.e("WordWidget", "Couldn't parse API response", e);
            }

           Pattern pattern = Pattern.compile(SimpleWikiHelper.WORD_OF_DAY_REGEX); //正则表达式处理,有关定义见下面的SimpleWikiHelper类
            Matchermatcher = pattern.matcher(pageContent);
            if(matcher.find()) {
               updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_word);

               String wordTitle = matcher.group(1);
               updateViews.setTextViewText(R.id.word_title, wordTitle);
               updateViews.setTextViewText(R.id.word_type, matcher.group(2));
               updateViews.setTextViewText(R.id.definition, matcher.group(3).trim());

               String definePage = res.getString(R.string.template_define_url,
                       Uri.encode(wordTitle));
               Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage)); //这里是打开相应的网页,所以Uri是http的url,action是view即打开web浏览器
               PendingIntent pendingIntent = PendingIntent.getActivity(context,
                       0 /* no requestCode */, defineIntent, 0 /* no flags */);
               updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent); //单击Widget打开Activity

           } else {
               updateViews = new RemoteViews(context.getPackageName(),R.layout.widget_message);
               CharSequence errorMessage = context.getText(R.string.widget_error);
               updateViews.setTextViewText(R.id.message, errorMessage);
            }
            returnupdateViews;
        }

        @Override
        public IBinder onBind(Intent intent){
            // We don'tneed to bind to this service
            return null;
        }
    }
}

  有关网络通讯的实体类,以及一些常量定义如下:

  public class SimpleWikiHelper {
    private static final String TAG ="SimpleWikiHelper";

    public static final StringWORD_OF_DAY_REGEX =
           "(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}";

    private static final StringWIKTIONARY_PAGE =
            "http://en.wiktionary.org/w/api.php?action=query&prop=revisions&titles=%s&" +
           "rvprop=content&format=json%s";

    private static final StringWIKTIONARY_EXPAND_TEMPLATES =
           "&rvexpandtemplates=true";

    private static final int HTTP_STATUS_OK= 200;

    private static byte[] sBuffer = newbyte[512];

    private static String sUserAgent = null;

     public static class ApiExceptionextends Exception {
        public ApiException(StringdetailMessage, Throwable throwable) {
            super(detailMessage,throwable);
        }

        publicApiException(String detailMessage) {
           super(detailMessage);
        }
    }

    public static class ParseExceptionextends Exception {
        public ParseException(StringdetailMessage, Throwable throwable) {
           super(detailMessage, throwable);
        }
    }

    public static voidprepareUserAgent(Context context) {
        try {
            // Readpackage name and version number from manifest
           PackageManager manager = context.getPackageManager();
            PackageInfoinfo = 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,"Couldn't find package information in PackageManager", e);
        }
    }

    public static StringgetPageContent(String title, boolean expandTemplates)
            throwsApiException, ParseException {
        String encodedTitle =Uri.encode(title);
        String expandClause =expandTemplates ? WIKTIONARY_EXPAND_TEMPLATES : "";

        String content =getUrlContent(String.format(WIKTIONARY_PAGE, encodedTitle, expandClause));
        try {
            JSONObjectresponse = new JSONObject(content);
            JSONObjectquery = response.getJSONObject("query");
            JSONObjectpages = query.getJSONObject("pages");
            JSONObjectpage = pages.getJSONObject((String) pages.keys().next());
            JSONArrayrevisions = page.getJSONArray("revisions");
            JSONObjectrevision = revisions.getJSONObject(0);
            returnrevision.getString("*");
        } catch (JSONException e) {
            throw new ParseException("Problemparsing API response", e);
        }
    }

    protected static synchronized StringgetUrlContent(String url) throws ApiException {
        if (sUserAgent == null) {
            throw newApiException("User-Agent string must be prepared");
        }

        HttpClientclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
       request.setHeader("User-Agent", sUserAgent); //设置客户端标识

        try {
            HttpResponseresponse = client.execute(request);

           StatusLine status = response.getStatusLine();
            if(status.getStatusCode() != HTTP_STATUS_OK) {
               throw new ApiException("Invalid response from server: " +
                       status.toString());
            }

           HttpEntity entity = response.getEntity();
            InputStreaminputStream = 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 newApiException("Problem communicating with API", e);
        }
    }
}

有关整个每日维基的widget例子比较简单,主要是帮助大家积累常用代码,了解Android平台 JSON的处理方式,毕竟很多Server还是Java的。

抱歉!评论已关闭.