Eros Live
Find the Way
posts - 15,comments - 0,trackbacks - 0

在manifest的activity节点使用

<activity android:windowSoftInputMode="adjustResize" . . . >

当点击EditText控件弹出软键盘的时候,系统会自动调整控件的位置。

代码

http://github.com/shaobin0604/miscandroidapps/tree/master/WindowSoftInputMode/

参考

posted @ 2010-08-25 18:42 Eros 阅读(257) | 评论 (0)编辑 收藏

AppWidget的初始化有两种方式:

  1. 没有提供Configure Activity, 则在 AppWidgetProvider#onUpdate 里初始化。
  2. 提供Configure Activity, 则在 Configure Activity 里初始化。

目前遇到的问题是:

在Launcher里可以预先配置桌面显示的AppWidget,如果AppWidget有Configure Activity,则系统在AppWidget的初始化过程不会发送android.appwidget.action.APPWIDGET_CONFIGURE Intent,而只是加载appwidget-provider里配置的initialLayout。这样第二种就不可用,只能用第一种方法。

posted @ 2010-08-24 11:11 Eros 阅读(424) | 评论 (0)编辑 收藏

1.字体大小

synchronized void
setTextSize(WebSettings.TextSize t)

Set the text size of the page.

2.缩放比例

void
setSupportZoom(boolean support)

Set whether the WebView supports zoom

void
setInitialScale(int scaleInPercent)

Set the initial scale for the WebView.

boolean
zoomIn()

Perform zoom in in the webview

boolean
zoomOut()

Perform zoom out in the webview

3.缩放控件

void
setBuiltInZoomControls(boolean enabled)

Sets whether the zoom mechanism built into WebView is used.

4.JavaScript支持

synchronized void
setJavaScriptEnabled(boolean flag)

Tell the WebView to enable javascript execution.

posted @ 2010-07-28 12:49 Eros 阅读(377) | 评论 (0)编辑 收藏

在程序里备份恢复数据

public static boolean backupDatabase() {
    File dbFile = new File(Environment.getDataDirectory() + "/data/" + PKG + "/databases/" + DB_NAME);
 
    File exportDir = new File(Environment.getExternalStorageDirectory(), "pocket-voa");
    
    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }
    
    File file = new File(exportDir, dbFile.getName());
 
    try {
        file.createNewFile();
        copyFile(dbFile, file);
        return true;
    } catch (IOException e) {
        Log.e(TAG, "[backupDatabase] error", e);
        return false;
    }
}
 
public static boolean restoreDatabase() {
    File dbFile = new File(Environment.getDataDirectory() + "/data/" + PKG + "/databases/" + DatabaseHelper.DB_NAME);
 
    File exportDbFile = new File(Environment.getExternalStorageDirectory() + "/pocket-voa/" + DatabaseHelper.DB_NAME);
    
    if (!exportDbFile.exists())
        return false;
 
    try {
        dbFile.createNewFile();
        copyFile(exportDbFile, dbFile);
        return true;
    } catch (IOException e) {
        Log.e(TAG, "[restoreDatabase] error", e);
        return false;
    }
}
 
private static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

参考

posted @ 2010-07-26 17:24 Eros 阅读(332) | 评论 (0)编辑 收藏

 

There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.

So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".

posted @ 2010-07-22 19:59 Eros 阅读(1162) | 评论 (0)编辑 收藏
     摘要: 1.海词 http://api.dict.cn/ws.php?utf8=true&q=#{word} 返回格式XML<?xml version="1.0" encoding="UTF-8" ?> <dict> <key>word</key> <lang>ec</lang> <audio>...  阅读全文
posted @ 2010-07-15 15:45 Eros 阅读(2020) | 评论 (0)编辑 收藏

打开终端输入

adb devices

出现如下内容

??????????? no permissions

原因是启动adb的时候需要有root权限。如果一开始忘记加了sudo, 就必须先终止adb。

$ adb kill-server

$ sudo adb start-server

$ adb devices

就可以看到设备信息了。

参考

posted @ 2010-07-07 15:44 Eros 阅读(588) | 评论 (0)编辑 收藏
     摘要: 原理 关闭APN的原理是在APN信息表(content://telephony/carriers/current)的apn, type字段添加自定义的后缀(参考自APNDroid) 代码 (取自 Quick Settings) package com.android.settings.widget; import android.content.ContentResolver;impo...  阅读全文
posted @ 2010-07-07 15:28 Eros 阅读(710) | 评论 (0)编辑 收藏

环境

Ubuntu 9.10 64bit, sun-jdk-1.5(因需要编译Android源代码), Android SDK 2.1

症状

draw9patch 不能正确显示出窗口,没有菜单栏

原因

sun jdk 1.5 的BUG

解决

安装 sun jdk 1.6

参考

  1. http://resources2.visual-paradigm.com/index.php/tips-support/53-support/61-blank-screen.html
  2. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6585673
  3. https://bugs.launchpad.net/ubuntu/+bug/164004
  4. http://forums.visual-paradigm.com/posts/list/6719.html
posted @ 2010-07-07 12:19 Eros 阅读(263) | 评论 (0)编辑 收藏

Install Ubuntu 9.10


Links

  1. http://docs.google.com/fileview?id=0B7vaQCSPJU8PNjUzZmU1ZTItYTVlNi00ZDBmLWFhMzMtN2Q3NDA4MzljMjRm&hl=zh_CN
  2. http://ubuntuabc.com/123/?p=38

Install JDK

sudo apt-get install sun-java6-jdk

sudo update-alternatives –config java


posted @ 2010-07-02 19:58 Eros 阅读(283) | 评论 (0)编辑 收藏

1.NPR News

http://code.google.com/p/npr-android-app/

image

2.Quick Settings

Quick Settings is a highly customizable all-in-one settings applications for Android.

http://code.google.com/p/quick-settings/

image

3.Quick Battery

http://code.google.com/p/quick-settings/source/browse/#svn/trunk/quick-battery

posted @ 2010-07-01 18:52 Eros 阅读(139) | 评论 (0)编辑 收藏

1.文本编辑器

 

2.文本查看器

JSON

3.正则表达式

在线测试工具

4.字体

YaHei Consolas

http://www.box.net/shared/72dcnre8on

设置字体大小为五号

参考http://be-evil.org/post-178.html

posted @ 2010-06-30 18:51 Eros 阅读(136) | 评论 (0)编辑 收藏

1.获取屏幕的分辨率

在 Activity 里使用如下代码,宽度和高度的单位是像素

Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

2.绘制文本

使用 FontMetrics 类

参考

http://www.javaeye.com/topic/474526

3.禁止自动横竖屏切换

在AndroidManifest.xml的Activity节点加入如下属性

android:screenOrientation="portrait"

portrait是纵向,landscape是横向

4.Resources and Assets

无论是使用Res\raw还是使用Asset存储资源文件,文件大小UNCOMPRESS限制为1MB

参考

http://wayfarer.javaeye.com/blog/547174

5.SDK 1.6 新增加SD卡写入权限

在AndroidManifest.xml加入以下代码

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

SDK1.6之前的项目自适应,无需此权限也可以写入SD卡。

6.在eclipse中查看Android Framework源代码

代码下载

  1. SDK 1.5 http://www.box.net/shared/16au19tqlp
  2. SDK 1.6 http://www.box.net/shared/dh4dr9ir7j
  3. SDK 2.2 http://www.box.net/shared/dic2t0blj1

参考

http://www.javaeye.com/topic/534010

7.给View注册ContextMenu

void setOnCreateContextMenuListener(View.OnCreateContextMenuListener l)

Register a callback to be invoked when the context menu for this view is being built.

8.给Preference设置Intent

void setIntent(Intent intent)

Sets an Intent to be used for startActivity(Intent) when this Preference is clicked.

9.包含CheckBox的ListView

ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"

10.取得当前的Locale

Locale locale = context.getResources().getConfiguration().locale;

11.使用android.text.format.Time类代替java.util.Calendar类

The Time class is a faster replacement for the java.util.Calendar and java.util.GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision. It is modelled after struct tm, and in fact, uses struct tm to implement most of the functionality.

12.调整WebView字体大小

WebView.getSettings().setDefaultFontSize()
WebView.getSettings().setDefaultZoom()

13.View Animation总结

参考

14.检查网络状态

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    
    return (info != null && info.isConnected());
}
 
 
public static boolean isWifiConnected(Context context) { 
    return getNetworkState(context, ConnectivityManager.TYPE_WIFI) == State.CONNECTED;
}
 
public static boolean isMobileConnected(Context context) {
    return getNetworkState(context, ConnectivityManager.TYPE_MOBILE) == State.CONNECTED;
}
 
private static State getNetworkState(Context context, int networkType) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo info = cm.getNetworkInfo(networkType);
    
    return info == null ? null : info.getState();    
}

需要加入权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

15.Parse JSON String

参考

  1. http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
  2. http://wiki.fasterxml.com/JacksonInFiveMinutes
  3. http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android

16.设置EditText的输入模式

url, password, email, 电话键盘等

设置 android:inputType 属性

17.Crash Report

  1. http://code.google.com/p/acra/
  2. http://code.google.com/p/android-send-me-logs/

18.用户解锁消息

android.intent.action.USER_PRESENT

只能在代码里注册Receiver

19.屏幕消息

android.intent.action.SCREEN_ON

android.intent.action.SCREEN_OFF

只能在代码里注册Receiver

posted @ 2010-06-29 13:22 Eros 阅读(496) | 评论 (0)编辑 收藏

实现应用《cnBeta业界资讯》分批加载数据的效果

image image image

这里ListView底部的“显示后10条”Button用到了ListView的方法

public void addFooterView (View v)

布局文件 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:id="@+id/list" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>
</LinearLayout>

MainActivity.java

package com.example.listviewwithheaderandfooter;
 
import java.util.ArrayList;
import java.util.List;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
 
public class MainActivity extends Activity {
    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
        "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
        "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
        "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
        "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
        "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
        "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
        "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
        "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
        "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
        "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
        "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
        "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
        "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
        "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
        "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
        "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
        "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
        "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
        "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
        "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
        "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
        "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
        "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
        "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
        "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
        "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
        "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
        "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
        "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
        "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
        "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
        "Ukraine", "United Arab Emirates", "United Kingdom",
        "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
        "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
      };
    
    private List<String> mList = new ArrayList<String>();
 
    private ArrayAdapter<String> mAdapter;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ListView list = (ListView) findViewById(R.id.list);
        
        Button buttonHeader = new Button(this);
        buttonHeader.setText("remove 1 entries");
        buttonHeader.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (mList.size() > 0) {
                    mList.remove(mList.size() - 1);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
        list.addHeaderView(buttonHeader);
        
        Button buttonFooter = new Button(this);
        buttonFooter.setText("add 1 entries");
        
        buttonFooter.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setIndeterminate(true);
                dialog.show();
                new Thread() {
                    public void run() {
                        int start = mList.size();
                        int end = start + 1;
                        if (end > COUNTRIES.length)
                            end = COUNTRIES.length;
                        
                        for (; start < end; start++) {
                            mList.add(COUNTRIES[start]);
                        }
                        
                        runOnUiThread(new Runnable() {
                            
                            @Override
                            public void run() {
                                dialog.dismiss();
                                mAdapter.notifyDataSetChanged();
                            }
                        });
                        
                    }
                }.start();
                
                
            }
        });
        list.addFooterView(buttonFooter);
        
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
        
        list.setAdapter(mAdapter);
    }
}

效果

image image

posted @ 2010-06-29 11:20 Eros 阅读(2916) | 评论 (0)编辑 收藏

http://stackoverflow.com/questions/2691570/android-how-to-properly-handle-onpause-onresume-methods

http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state

http://stackoverflow.com/questions/2441145/onpause-onresume-activity-issues

 

sprite

posted @ 2010-05-21 09:45 Eros 阅读(108) | 评论 (0)编辑 收藏