Android 学习笔记

本笔记记录在工作学习中的小心得,并随时更新。

 

2010-8-6

界面中的小白色分隔符。今天要设计界面,与系统自带的界面相比,我自己做的太丑陋了。寻找了一下,有两种方法,一个是listview的divider,另一个是PreferenceScreen。

 

2010-8-7

preferenceScreen的例子:http://www.kaloer.com/android-preferences

 

一些代码可以参考的:

package com.comapping.android.preferences; import com.comapping.android.Constants; import com.comapping.android.R; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.Preference.OnPreferenceChangeListener; public class PreferencesActivity extends PreferenceActivity { public final static String PREFERENCES_ACTIVITY_INTENT = "com.comapping.android.intent.PREFERENCES"; private void initPreference(Preference preference, CharSequence defaultValue) { preference.setSummary(defaultValue); preference .setOnPreferenceChangeListener(new OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { preference.setSummary((CharSequence) newValue); return true; } }); } private void initListPreference(ListPreference preference, String defaultValue) { if (preference.getEntry() == null) { // set default value preference.setValue(defaultValue); } initPreference(preference, preference.getEntry()); } private void initTextPreference(EditTextPreference preference, String defaultValue) { if (preference.getText() == null) { preference.setText(defaultValue); } initPreference(preference, preference.getText()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.layout.preferences); // init view type preference initListPreference((ListPreference) findPreference("viewType"), Constants.VIEW_TYPE_COMAPPING); // init download preference initTextPreference( (EditTextPreference) findPreference("downloadFolder"), PreferencesStorage.DOWNLOAD_FOLDER_DEFAULT_VALUE); // init proxy preference // proxy host initTextPreference((EditTextPreference) findPreference("proxyHost"), ""); // proxy port initTextPreference((EditTextPreference) findPreference("proxyPort"), ""); // proxy name initTextPreference( (EditTextPreference) findPreference("proxyAuthUserName"), ""); // proxy password initTextPreference( (EditTextPreference) findPreference("proxyAuthUserPassword"), ""); } } 

 

2010-8-7

preferenceActivity参考:http://www.ideasandroid.com/?p=284

 

2010-8-9

listview参考资料:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

国外blog:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

 

2010-8-10

关于preferenceActivity最好的例子是sdk中自带的apidemo,

这里有很多实用的例子。值得花时间来看。

关于listview,这篇文章很好:http://www.ophonesdn.com/article/show/112

国外一篇blog也不错:http://thebogles.com/blog/2010/02/section-headers-for-android-listviews/

 

2010-8-17

1、关于模拟器访问电脑本机:在模拟器上用10.0.2.2访问你的电脑本机。

 

2、获得手机ip

public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; } 

3、java各类型间的互换

//各种数字类型转换成字符串型: Strings=String.valueOf(value);//其中value为任意一种数字类型。 //字符串型转换成各种数字类型: Strings="169"; byteb=Byte.parseByte(s); shortt=Short.parseShort(s); inti=Integer.parseInt(s); longl=Long.parseLong(s); Floatf=Float.parseFloat(s); Doubled=Double.parseDouble(s); //数字类型与数字类对象之间的转换: byteb=169; Bytebo=newByte(b); b=bo.byteValue(); shortt=169; Shortto=newShort(t); t=to.shortValue(); inti=169; Integerio=newInteger(i); i=io.intValue(); longl=169; Longlo=newLong(l); l=lo.longValue(); floatf=169f; Floatfo=newFloat(f); f=fo.floatValue(); doubled=169f; DoubledObj=newDouble(d); d=dObj.doubleValue();  

 

2010-8-24

IBM网站上一篇activity的帖子:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/index.html

 

2010-8-26

介绍tabactivity的文章:http://fatkun.com/2010/06/android-tabwidget.html

 

2010-8-27

另一篇介绍tab页面的文章:http://www.pocketdigi.com/20100824/71.html

 

2010-8-28

介绍java中时间处理的文章:http://alexfc.javaeye.com/blog/363185

另一篇介绍tab标签的文章:http://www.javaeye.com/topic/602737

 

2010-8-31

控件获得焦点:用requestFocus()方法

 

2010-9-14

获得屏幕分辨率:

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

 

2010-9-16

package org.apache.http.examples.client; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class ClientExecuteProxy { public static void main(String[] args)throws Exception { HttpHost proxy = new HttpHost( "10.0.0.172", 80, "http"); HttpHost target = new HttpHost("YOUR_TARGET_IP", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target + " via " + proxy); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i<headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } } 

 

intent用法

//Android通过程序打开MP3播放器播放音乐 Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/song.MP3"); it.setDataAndType(uri, "audio/MP3"); startActivity(it); Intent it = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/song.MP3"); it.setDataAndType(uri, "audio/MP3"); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //Uninstall 程序 : Uri uri = Uri.fromParts("package", strPackageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivity(it); Uri uri = Uri.fromParts("package", strPackageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivity(it); //发送彩信 Uri uri = Uri.parse("content://media/external/images/media/23"); Intent it = new Intent(Intent.ACTION_SEND); it.putExtra("sms_body", "some text"); it.putExtra(Intent.EXTRA_STREAM, uri); it.setType("image/png"); startActivity(it); Uri uri = Uri.parse("content://media/external/images/media/23"); Intent it = new Intent(Intent.ACTION_SEND); it.putExtra("sms_body", "some text"); it.putExtra(Intent.EXTRA_STREAM, uri); it.setType("image/png"); startActivity(it); //发送SMS/MMS //调用发送短信的程序 Intent it = new Intent(Intent.ACTION_VIEW); it.putExtra("sms_body", "The SMS text"); it.setType("vnd.Android-dir/mms-sms"); startActivity(it); Intent it = new Intent(Intent.ACTION_VIEW); it.putExtra("sms_body", "The SMS text"); it.setType("vnd.Android-dir/mms-sms"); startActivity(it); //发送短信 Uri uri = Uri.parse("smsto:0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "The SMS text"); startActivity(it); Uri uri = Uri.parse("smsto:0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "The SMS text"); startActivity(it); //拨打电话: //调用拨号程序 Uri uri = Uri.parse("tel:xxxxxx"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); Uri uri = Uri.parse("tel.xxxxxx"); Intent it =new Intent(Intent.ACTION_CALL,uri); Uri uri = Uri.parse("tel:xxxxxx"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); Uri uri = Uri.parse("tel.xxxxxx"); Intent it =new Intent(Intent.ACTION_CALL,uri); 要使用这个必须在配置文件中加入<uses-permission id="Android.permission.CALL_PHONE" /> //显示网页: Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); Uri uri = Uri.parse("http://www.google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); //显示地图: Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.Action_VIEW,uri); startActivity(it); Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.Action_VIEW,uri); startActivity(it); //路径规划: Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); Intent it = new Intent(Intent.ACTION_VIEW,URI); startActivity(it);  

 

2010-9-18

监听网络连接状态

final TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mTelephonyMgr.listen(new PhoneStateListener(){ @Override public void onDataConnectionStateChanged(int state) { switch(state){ case TelephonyManager.DATA_DISCONNECTED://网络断开 break; case TelephonyManager.DATA_CONNECTING://网络正在连接 break; case TelephonyManager.DATA_CONNECTED://网络连接上 break; } } }, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); //只要重载onDataConnectionStateChanged方法,根据state判断做相应的处理。 

 

2010-9-20

跳转到系统设置界面-网络连接

startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0); 

 

2011-6-21

用findViewById获取另一个layout中的view时,需要用otherLayout.findViewById的方式(第16行)。否则操作view的时候会出错。

import android.os.Bundle; import android.app.Activity; import android.widget.TextView; import android.view.LayoutInflater; import android.view.View; public class mainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LayoutInflater inflater = getLayoutInflater(); View otherLayout = inflater.inflate(R.layout.other, null); TextView otherTv1 = (TextView)otherLayout.findViewById(R.id.otherTV1); otherTv1.setText("11111"); } } 

 

你可能感兴趣的:(android,String,ListView,import,sms,2010)