|
在manifest的activity節(jié)點使用
<activity android:windowSoftInputMode="adjustResize" . . . >
當點擊EditText控件彈出軟鍵盤的時候,系統(tǒng)會自動調整控件的位置。
代碼
http://github.com/shaobin0604/miscandroidapps/tree/master/WindowSoftInputMode/
參考
AppWidget的初始化有兩種方式:
目前遇到的問題是:
在Launcher里可以預先配置桌面顯示的AppWidget,如果AppWidget有Configure Activity,則系統(tǒng)在AppWidget的初始化過程不會發(fā)送android.appwidget.action.APPWIDGET_CONFIGURE Intent,而只是加載appwidget-provider里配置的initialLayout。這樣第二種就不可用,只能用第一種方法。
synchronized void
setTextSize(WebSettings.TextSize t)
Set the text size of the page.
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
void
setBuiltInZoomControls(boolean enabled)
Sets whether the zoom mechanism built into WebView is used.
synchronized void
setJavaScriptEnabled(boolean flag)
Tell the WebView to enable javascript execution.
在程序里備份恢復數(shù)據(jù)
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();
}
}
參考
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".
打開終端輸入
adb devices
出現(xiàn)如下內容
??????????? no permissions
原因是啟動adb的時候需要有root權限。如果一開始忘記加了sudo, 就必須先終止adb。
$ adb kill-server
$ sudo adb start-server
$ adb devices
就可以看到設備信息了。
Ubuntu 9.10 64bit, sun-jdk-1.5(因需要編譯Android源代碼), Android SDK 2.1
draw9patch 不能正確顯示出窗口,沒有菜單欄
sun jdk 1.5 的BUG
安裝 sun jdk 1.6
sudo apt-get install sun-java6-jdk
sudo update-alternatives –config java
http://code.google.com/p/npr-android-app/
Quick Settings is a highly customizable all-in-one settings applications for Android.
http://code.google.com/p/quick-settings/
http://code.google.com/p/quick-settings/source/browse/#svn/trunk/quick-battery
http://www.box.net/shared/72dcnre8on
設置字體大小為五號
參考http://be-evil.org/post-178.html
在 Activity 里使用如下代碼,寬度和高度的單位是像素
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
使用 FontMetrics 類
參考
http://www.javaeye.com/topic/474526
在AndroidManifest.xml的Activity節(jié)點加入如下屬性
android:screenOrientation="portrait"
portrait是縱向,landscape是橫向
無論是使用Res\raw還是使用Asset存儲資源文件,文件大小UNCOMPRESS限制為1MB
參考
http://wayfarer.javaeye.com/blog/547174
在AndroidManifest.xml加入以下代碼
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
SDK1.6之前的項目自適應,無需此權限也可以寫入SD卡。
代碼下載
參考
http://www.javaeye.com/topic/534010
void setOnCreateContextMenuListener(View.OnCreateContextMenuListener l)
Register a callback to be invoked when the context menu for this view is being built.
Sets an Intent
to be used for startActivity(Intent)
when this Preference is clicked.
ListView item中加入checkbox后onListItemClick 事件無法觸發(fā)。
原因:checkbox的優(yōu)先級高于ListItem于是屏蔽了ListItem的單擊事件。
解決方案:設置checkbox的android:focusable="false"
Locale locale = context.getResources().getConfiguration().locale;
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.
WebView.getSettings().setDefaultFontSize()
WebView.getSettings().setDefaultZoom()
參考
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"/>
參考
url, password, email, 電話鍵盤等
設置 android:inputType 屬性
android.intent.action.USER_PRESENT
只能在代碼里注冊Receiver
android.intent.action.SCREEN_ON
android.intent.action.SCREEN_OFF
只能在代碼里注冊Receiver
實現(xiàn)應用《cnBeta業(yè)界資訊》分批加載數(shù)據(jù)的效果
這里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);
}
}
效果