aquarium

find the way

Display current value of an android preference in the preference summary 2010年06月22日

类归于: program — shaobin0604 @ 5:37 下午

 

参考

http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-sum

 

How to set more than one alarm 2010年06月18日

类归于: program — shaobin0604 @ 4:55 下午

方法一

参考AlarmClock应用,在响应Alarm的同时设置下一个Alarm

方法二

使用PendingIntent.getBroadcast()的requestCode参数。要想注册多几个Alarm服务,标识就要不同~

参考

http://www.eoeandroid.com/viewthread.php?tid=14650&highlight=

 

using xmlPullParser to Parse msn weather data 2010年06月11日

类归于: program — shaobin0604 @ 2:03 下午

原始XML文档

使用MSN天气WEB服务查询北京的天气返回的xml文档如下

<weatherdata>
  <weather weatherlocationcode="wc:CHXX0008" weatherlocationname="Beijing, CHN" zipcode="" encodedlocationname="Beijing%2c+CHN" url="http://weather.msn.com/local.aspx?wealocations=wc:CHXX0008&amp;q=Beijing%2c+CHN" imagerelativeurl="http://blst.msn.com/as/wea3/i/en-us/" degreetype="C" provider="Foreca" attribution="Data provided by Foreca" attribution2="© Foreca" lat="39.9125748" long="116.3889847" timezone="8" alert="">
    <current temperature="22" skycode="28" skytext="Mostly Cloudy" date="2010-06-11" day="Friday" shortday="Fri" observationtime="09:30:00" observationpoint="Tianjin / Zhangguizhu" feelslike="22" humidity="69" windspeed="11" winddisplay="11 km/hr SSE"/>
    <forecast low="19" high="27" skycodeday="26" skytextday="Cloudy" date="2010-06-11" day="Friday" shortday="Fri" precip="35"/>
    <forecast low="20" high="31" skycodeday="28" skytextday="PM Clouds" date="2010-06-12" day="Saturday" shortday="Sat" precip="10"/>
    <forecast low="20" high="31" skycodeday="26" skytextday="Cloudy" date="2010-06-13" day="Sunday" shortday="Sun" precip="60"/>
    <forecast low="22" high="30" skycodeday="34" skytextday="Fair" date="2010-06-14" day="Monday" shortday="Mon" precip="65"/>
    <forecast low="26" high="34" skycodeday="32" skytextday="Clear" date="2010-06-15" day="Tuesday" shortday="Tue" precip="25"/>
    <toolbar timewindow="60" minversion="1.0.1965.0"/>
  </weather>
</weatherdata>

解析代码

Java语言: Codee#11661
private void parseWeather() {
    mForecasts = new ArrayList<Map<String,String>>();
    mCondition = new HashMap<String, String>();
    InputStream is = null;
    try {
//    is = getLocalWeatherSource();
      is = getWebWeatherSource();
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser xpp = factory.newPullParser();
      xpp.setInput(is, "UTF-8");
      int eventType = xpp.getEventType();
      while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
          System.out.println("Start document");
        } else if (eventType == XmlPullParser.END_DOCUMENT) {
          System.out.println("End document");
        } else if (eventType == XmlPullParser.START_TAG) {
          String tagName = xpp.getName();
          System.out.println("Start tag " + tagName);
          if ("current".equals(tagName)) {
            mCondition.put("temperature", xpp.getAttributeValue(null, "temperature"));
            mCondition.put("skycode", xpp.getAttributeValue(null, "skycode"));
            mCondition.put("skytext", xpp.getAttributeValue(null, "skytext"));
            mCondition.put("date", xpp.getAttributeValue(null, "date"));
            mCondition.put("observationtime", xpp.getAttributeValue(null, "observationtime"));
            mCondition.put("observationpoint", xpp.getAttributeValue(null, "observationpoint"));
            mCondition.put("feelslike", xpp.getAttributeValue(null, "feelslike"));
            mCondition.put("humidity", xpp.getAttributeValue(null, "humidity"));
            mCondition.put("windspeed", xpp.getAttributeValue(null, "windspeed"));
          } else if ("forecast".equals(tagName)) {
            Map<String, String> forecast = new HashMap<String, String>();
            forecast.put("low", xpp.getAttributeValue(null, "low"));
            forecast.put("high", xpp.getAttributeValue(null, "high"));
            forecast.put("skycodeday", xpp.getAttributeValue(null, "skycodeday"));
            forecast.put("skytextday", xpp.getAttributeValue(null, "skytextday"));
            forecast.put("date", xpp.getAttributeValue(null, "date"));
            forecast.put("precip", xpp.getAttributeValue(null, "precip"));
            mForecasts.add(forecast);
          }
        } else if (eventType == XmlPullParser.END_TAG) {
          System.out.println("End tag " + xpp.getName());
        } else if (eventType == XmlPullParser.TEXT) {
          System.out.println("Text " + xpp.getText());
        }
        eventType = xpp.next();
      }
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          StringBuilder str = new StringBuilder();
          str.append("——- condition ——-\n");
          str.append(mCondition.toString());
          str.append(’\n’);
          str.append("——- forecasts ——-\n");
          for (Map<String, String> forecast : mForecasts) {
            str.append(forecast.get("date"));
            str.append(’\n’);
            str.append(forecast.toString());
            str.append("\n\n");
          }
          mProgressBar.setVisibility(View.GONE);
          mTextView.setVisibility(View.VISIBLE);
          mTextView.setText(str);
        }
      });
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (XmlPullParserException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      if (is != null)
        try {
          is.close();
        } catch (Exception e2) {
          e2.printStackTrace();
        }
    }
  }

代码下载

http://www.box.net/shared/ckc56arzlu

 

Fetion aPI resource

类归于: program — shaobin0604 @ 12:19 下午

目前比较稳定的飞信API

 

Yahoo aPI tips 2010年06月10日

类归于: program — shaobin0604 @ 5:34 下午

1. 通过地名获取WOEID

使用

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22Barrie%20CA%22&format=xml

参考

http://stackoverflow.com/questions/1822650/yahoo-weather-api-woeid-retrieval

 

2. 通过GPS坐标获取WOEID

http://query.yahooapis.com/v1/public/yql?q=select%20place.woeid%20from%20flickr.places%20where%20lat%3D43%20and%20lon%3D-94&format=xml

参考

http://stackoverflow.com/questions/2093358/receive-a-woeid-by-lat-long-with-yahoos-api

 

Doc for the return value of Service.onStartCommand

类归于: program — shaobin0604 @ 11:35 上午
public static final int START_CONTINUATION_MASK

Since: API Level 5

Bits returned by onStartCommand(Intent, int, int) describing how to continue the service if it is killed. May be START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT, or START_STICKY_COMPATIBILITY.

Constant Value: 15 (0×0000000f)

public static final int START_FLAG_REDELIVERY

Since: API Level 5

This flag is set in onStartCommand(Intent, int, int) if the Intent is a re-delivery of a previously delivered intent, because the service had previously returned START_REDELIVER_INTENT but had been killed before calling stopSelf(int) for that Intent.

Constant Value: 1 (0×00000001)

public static final int START_FLAG_RETRY

Since: API Level 5

This flag is set in onStartCommand(Intent, int, int) if the Intent is a a retry because the original attempt never got to or returned from onStartCommand(Intent, int, int).

Constant Value: 2 (0×00000002)

public static final int START_NOT_STICKY

Since: API Level 5

Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don’t recreate until a future explicit call to Context.startService(Intent). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.

This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work. An example of such a service would be one that polls for data from a server: it could schedule an alarm to poll every N minutes by having the alarm start its service. When its onStartCommand(Intent, int, int) is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. If its process is killed while doing that check, the service will not be restarted until the alarm goes off.

Constant Value: 2 (0×00000002)

public static final int START_REDELIVER_INTENT

Since: API Level 5

Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).

Constant Value: 3 (0×00000003)

public static final int START_STICKY

Since: API Level 5

Constant to return from onStartCommand(Intent, int, int): if this service’s process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don’t retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

Constant Value: 1 (0×00000001)

public static final int START_STICKY_COMPATIBILITY

Since: API Level 5

Constant to return from onStartCommand(Intent, int, int): compatibility version of START_STICKY that does not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.

Constant Value: 0 (0×00000000)

 

Intent. ACTION_DATE_CHANGED does not fire when the date changes. 2010年06月8日

类归于: program — shaobin0604 @ 12:39 下午

最近做一个日历相关的Widget,需要在日期变化的时候更新Widget的显示。查找Intent的文档,发现貌似ACTION_DATE_CHANGED可以实现我想要的功能。但实际上该Intent并不会发送。

参考

 

Performance issue caused by String.format in android 2010年05月28日

类归于: program — shaobin0604 @ 3:58 下午

测试程序

package com.example;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar calendar = Calendar.getInstance();
        
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < 30; i++) {
            long start = System.currentTimeMillis();
            String.format("%d-%02d-%02d", year, month, day);
            long end = System.currentTimeMillis();
            Log.d("Main", "String.format cost time: " + (end - start) + " millis");
        }
    }
}

Log记录

05-28 16:03:39.685: DEBUG/Main(15854): String.format cost time: 22 millis
05-28 16:03:39.705: DEBUG/Main(15854): String.format cost time: 15 millis
05-28 16:03:39.725: DEBUG/Main(15854): String.format cost time: 17 millis
05-28 16:03:39.745: DEBUG/Main(15854): String.format cost time: 14 millis
05-28 16:03:39.755: DEBUG/Main(15854): String.format cost time: 14 millis
05-28 16:03:39.775: DEBUG/Main(15854): String.format cost time: 14 millis
05-28 16:03:39.785: DEBUG/Main(15854): String.format cost time: 14 millis
05-28 16:03:39.805: DEBUG/Main(15854): String.format cost time: 14 millis
05-28 16:03:39.825: DEBUG/Main(15854): String.format cost time: 15 millis
05-28 16:03:39.835: DEBUG/Main(15854): String.format cost time: 13 millis

java.lang.String#format 方法在我的G1上面大概会花费 15 millis。

 

access database via adb

类归于: program — shaobin0604 @ 3:04 下午

1. 启动模拟器

2. 进入 adb shell

3. cd /data/data, ls 可以看见已安装的应用的数据目录

# ls

com.youdao.dict

com.kingsoft.android

com.keenvim.cnCalendar

Remword.Panso

4. cd Remword.Panso/databases, ls 可见当前应用下所有的database files。

# ls

dict

books

wordbook

5. sqlite3 books

# sqlite3 books
sqlite3 books
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> .tables
.tables
CurrentBook       WordBook          android_metadata
sqlite> select * from WordBook;
select * from WordBook;
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|abbreviation|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|abide|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|abolish|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|absent|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|absorption|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|abstract|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|absurd|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|abundance|1
19e8f16b-bc4f-4a00-ad2b-31ae0d456139|accessory|1

 

 

layout with header search bar and footer tool bar 2010年05月25日

类归于: program — shaobin0604 @ 3:14 下午

Source

main.xml

XML语言: Codee#11254
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <include layout="@layout/search" />
    <include layout="@layout/content" />
    <include layout="@layout/footbar" />
</LinearLayout>

search.xml

XML语言: Codee#11255
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal" android:id="@+id/search_panel"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/dark_grey" >
    <AutoCompleteTextView android:layout_gravity="center_vertical"
        android:id="@+id/key" android:layout_width="0.0dip"
        android:layout_height="wrap_content" android:layout_marginLeft="8.0dip"
        android:layout_marginTop="3.0dip" android:layout_marginRight="4.0dip"
        android:singleLine="true" android:layout_weight="1.0" />
    <ImageButton android:layout_gravity="center_vertical" android:id="@+id/search"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="1.0dip" android:layout_marginTop="2.0dip"
        android:layout_marginRight="6.0dip" android:src="@android:drawable/ic_search_category_default" />
</LinearLayout>

content.xml

XML语言: Codee#11256
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0">
    <TextView android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10.0dip"
        android:layout_marginRight="8.0dip" />
</LinearLayout>

footbar.xml

XML语言: Codee#11257
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:background="@color/dark_grey"
    android:paddingTop="3dip" >
    <Button android:id="@+id/prev" android:layout_height="wrap_content"
        android:layout_width="0.0dip" android:layout_weight="0.3"
        android:gravity="center"
        android:text="@string/btn_prev" />
    <Button android:id="@+id/auto" android:layout_height="wrap_content"
        android:layout_width="0.0dip" android:layout_weight="0.3"
        android:gravity="center"
        android:text="@string/btn_auto"/>
    <Button android:id="@+id/next" android:layout_height="wrap_content"
        android:layout_width="0.0dip" android:layout_weight="0.3"
        android:gravity="center"
        android:text="@string/btn_next"/>
</LinearLayout>

colors.xml

XML语言: Codee#11258
<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color name="light_grey">#ffe0e0e0</color>
    <color name="dark_grey">#ff989898</color>
    <color name="black">#ff000000</color>
    <color name="backcolor">#ff181818</color>
    <color name="white">#ffffffff</color>
</resources>

strings.xml

XML语言: Codee#11259
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">Memo</string>
   
    <string name="btn_prev">上一个</string>
    <string name="btn_next">下一个</string>
    <string name="btn_auto">自动播放</string>
   
</resources>

Screenshot

image