当前位置: 编程技术>移动开发
本页文章导读:
▪取得联系人姓名,相关电话以及邮件地址sdk2.0 获得联系人姓名,相关电话以及邮件地址sdk2.0
权限别忘记
<uses-permission android:name="android.permission.READ_CONTACTS" />
创建一个抽象类 并判断版本问题
abstract class ContactsAdapterBridge {
abstract Lis.........
▪ listView中扩张ArrayAdapter,以及view的使用 listView中扩展ArrayAdapter,以及view的使用
public class SelectorDemo extends ListActivity {
private static ColorStateList allWhite=ColorStateList.valueOf(0xFFFF0000);
private static String[] items={"lorem", "ipsum", "dolor","purus"};
@Ov.........
▪ C/S通信,互传文件 C/S通讯,互传文件
上传或者下载文件客户端:package com.et.FileClient;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.........
[1]取得联系人姓名,相关电话以及邮件地址sdk2.0
来源: 互联网 发布时间: 2014-02-18
获得联系人姓名,相关电话以及邮件地址sdk2.0
权限别忘记
<uses-permission android:name="android.permission.READ_CONTACTS" />
创建一个抽象类 并判断版本问题
abstract class ContactsAdapterBridge { abstract ListAdapter buildNameAdapter(Activity a); abstract ListAdapter buildPhonesAdapter(Activity a); abstract ListAdapter buildEmailAdapter(Activity a); public static final ContactsAdapterBridge INSTANCE=buildBridge(); private static ContactsAdapterBridge buildBridge() { int sdk=new Integer(Build.VERSION.SDK).intValue(); if (sdk<5) { return(new OldContactsAdapterBridge()); } return(new NewContactsAdapterBridge()); } }
实现这个类
class NewContactsAdapterBridge extends ContactsAdapterBridge { ListAdapter buildNameAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID, Contacts.DISPLAY_NAME,}; Cursor c=a.managedQuery(Contacts.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_1, c, new String[] { Contacts.DISPLAY_NAME }, new int[] { android.R.id.text1 })); } ListAdapter buildPhonesAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER }; Cursor c=a.managedQuery(Phone.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER }, new int[] { android.R.id.text1, android.R.id.text2 })); } ListAdapter buildEmailAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID,Contacts.DISPLAY_NAME,Email.DATA}; Cursor c=a.managedQuery(Email.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.DISPLAY_NAME, Email.DATA }, new int[] { android.R.id.text1, android.R.id.text2 })); } }
注意不同颜色就好了。
public class ContactSpinners extends ListActivity implements AdapterView.OnItemSelectedListener { private static String[] options={"Contact Names","Contact Names & Numbers","Contact Names & Email Addresses"}; private ListAdapter[] listAdapters=new ListAdapter[3]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initListAdapters(); Spinner spin=(Spinner)findViewById(R.id.spinner); spin.setOnItemSelectedListener(this); ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, options); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(aa); } public void onItemSelected(AdapterView<?> parent,View v, int position, long id) { setListAdapter(listAdapters[position]); } public void onNothingSelected(AdapterView<?> parent) { // ignore } private void initListAdapters() { listAdapters[0]=ContactsAdapterBridge.INSTANCE.buildNameAdapter(this); listAdapters[1]=ContactsAdapterBridge.INSTANCE.buildPhonesAdapter(this); listAdapters[2]=ContactsAdapterBridge.INSTANCE.buildEmailAdapter(this); } }
主程序就很简单了。
最后就是以前版本的查找联系人
class OldContactsAdapterBridge extends ContactsAdapterBridge { ListAdapter buildNameAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.People._ID, Contacts.PeopleColumns.NAME }; Cursor c=a.managedQuery(Contacts.People.CONTENT_URI, PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_1, c, new String[] { Contacts.PeopleColumns.NAME }, new int[] { android.R.id.text1 })); } ListAdapter buildPhonesAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.Phones._ID, Contacts.Phones.NAME, Contacts.Phones.NUMBER }; Cursor c=a.managedQuery(Contacts.Phones.CONTENT_URI, PROJECTION, null, null, Contacts.Phones.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.Phones.NAME, Contacts.Phones.NUMBER }, new int[] { android.R.id.text1, android.R.id.text2 })); } ListAdapter buildEmailAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.ContactMethods._ID, Contacts.ContactMethods.DATA, Contacts.PeopleColumns.NAME }; Cursor c=a.managedQuery(Contacts.ContactMethods.CONTENT_EMAIL_URI, PROJECTION, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.PeopleColumns.NAME, Contacts.ContactMethods.DATA }, new int[] { android.R.id.text1, android.R.id.text2 })); } }
要导入的包
import android.database.Cursor; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone;
[2] listView中扩张ArrayAdapter,以及view的使用
来源: 互联网 发布时间: 2014-02-18
listView中扩展ArrayAdapter,以及view的使用
public class SelectorDemo extends ListActivity { private static ColorStateList allWhite=ColorStateList.valueOf(0xFFFF0000); private static String[] items={"lorem", "ipsum", "dolor","purus"}; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); setListAdapter(new SelectorAdapter(this)); getListView().setOnItemSelectedListener(listener); } class SelectorAdapter extends ArrayAdapter { SelectorAdapter(Context ctxt) { super(ctxt,R.layout.row, items); } @Override public View getView(int position, View convertView,ViewGroup parent) { SelectorWrapper wrapper=null; if (convertView==null) { convertView=getLayoutInflater().inflate(R.layout.row,parent, false); wrapper=new SelectorWrapper(convertView); wrapper.getLabel().setTextColor(allWhite); convertView.setTag(wrapper); } else { wrapper=(SelectorWrapper)convertView.getTag(); } wrapper.getLabel().setText(items[position]); return(convertView); } } class SelectorWrapper { View row=null; TextView label=null; View bar=null; SelectorWrapper(View row) { this.row=row; } TextView getLabel() { if (label==null) { label=(TextView)row.findViewById(R.id.label); } return(label); } View getBar() { if (bar==null) { bar=row.findViewById(R.id.bar); } return(bar); } } AdapterView.OnItemSelectedListener listener=new AdapterView.OnItemSelectedListener() { View lastRow=null; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.i("view","parent:"+parent+"view:"+view+position); if (lastRow!=null) { SelectorWrapper wrapper=(SelectorWrapper)lastRow.getTag(); wrapper.getBar().setVisibility(View.INVISIBLE); } SelectorWrapper wrapper=(SelectorWrapper)view.getTag(); wrapper.getBar().setVisibility(View.VISIBLE); lastRow=view; } public void onNothingSelected(AdapterView<?> parent) { //Log.i("onNothingSelected","parent:"+parent); if (lastRow!=null) { SelectorWrapper wrapper=(SelectorWrapper)lastRow.getTag(); wrapper.getBar().setVisibility(View.INVISIBLE); lastRow=null; } } }; }
main
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:listSelector="#00000000" />
最关键的就是下面的了
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <View android:id="@+id/bar" android:background="#FFFFFF00" android:layout_width="5px" android:layout_height="fill_parent" android:visibility="invisible" /> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="10pt" android:paddingTop="2px" android:paddingBottom="2px" android:paddingLeft="5px" /> </LinearLayout>
这个view就是一条横线了而是一条竖线
[3] C/S通信,互传文件
来源: 互联网 发布时间: 2014-02-18
C/S通讯,互传文件
上传或者下载文件客户端:
下载文件服务器端:
上传文件服务器端:
上传或者下载文件客户端:
package com.et.FileClient; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FileClient extends Activity { /** Called when the activity is first created. */ private TextView textView; private Button download; private Button upload; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView=(TextView) findViewById(R.id.textview); download=(Button) findViewById(R.id.Button01); upload = (Button) findViewById(R.id.Button02); download.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub try { Socket s=new Socket("116.29.62.23",5554); InputStream is=s.getInputStream(); if (is==null) { throw new RuntimeException("stream is null"); } File myTempFile = new File("/data/data/com.et.FileClient","a.jpg"); FileOutputStream fos = new FileOutputStream(myTempFile); byte buf[]=new byte[1024]; while (true) { int num = is.read(buf); if (num<=0) break; fos.write(buf, 0, num); } is.close(); s.close(); } catch(Exception e) { System.out.println("发生异常:"+e); e.printStackTrace(); } } }); upload.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub try { Socket s=new Socket("116.29.62.23",5554); File file = new File("/data/data/com.et.FileClient/image.jpg"); FileInputStream fos = new FileInputStream(file); DataOutputStream out = new DataOutputStream(s.getOutputStream()); byte[] buf = new byte[1024]; int num = fos.read(buf); while (num!=-1) { out.write(buf, 0, num); out.flush(); num=fos.read(buf); } } catch(Exception e) { } } }); } }
下载文件服务器端:
package com.et.FileClient; import java.io.*; import java.util.*; import java.net.*; public class FileDownServer { public static void main(String[] args) throws Exception { //创建文件流用来读取文件中的数据 File file = new File("a.jpg"); FileInputStream fos = new FileInputStream(file); ServerSocket ss = new ServerSocket(5554); //创建网络服务器接受客户请求 while (true) { Socket client = ss.accept(); System.out.println("accept"); //创建网络输出流并提供数据包装器 OutputStream netOut = client.getOutputStream(); OutputStream doc = new DataOutputStream( new BufferedOutputStream(netOut)); //创建文件读取缓冲区 byte[] buf = new byte[2048]; int num = fos.read(buf); while (num != (-1)) {//是否读完文件 doc.write(buf, 0, num);//把文件数据写出网络缓冲区 doc.flush();//刷新缓冲区把数据写往客户端 num = fos.read(buf);//继续从文件中读取数据 } fos.close(); doc.close(); } } }
上传文件服务器端:
package com.et.FileClient; import java.io.*; import java.util.*; import java.net.*; public class FileUpServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(5554); //创建网络服务器接受客户请求 while (true) { Socket s = ss.accept(); System.out.println("accept"); //创建网络输出流并提供数据包装器 InputStream is=s.getInputStream(); if (is==null) { throw new RuntimeException("stream is null"); } File myTempFile = new File("image.jpg"); FileOutputStream fos = new FileOutputStream(myTempFile); byte buf[]=new byte[1024]; while (true) { int num = is.read(buf); if (num<=0) break; fos.write(buf, 0, num); } is.close(); s.close(); } } }
最新技术文章: