RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
ContentProvider浅谈+实例-创新互联

作为安卓四大组件之一,ContentProvider的用处也不少,ContentProvider用于保存和检索数据,是安卓系统中不同应用程序之间共享数据的接口。

成都创新互联从2013年成立,先为沁县等服务建站,沁县等地企业,进行企业商务咨询服务。为沁县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

  最直观的应用就是当你发送短信时需要用到联系人的相关信息,此时通过ContentProvider提供的接口访问Android系统中的电话簿,并从中选中了联系人。

  Android系统对一系列公共的常用数据类型提供了对应的ContentProvider接口,都定义在android.provider包下。

  ContentProvider实现共享数据:ContentProvider提供了一组应用程序之间能访问的接口,应用程序通过ContentProvider把当前应用中的数据共享给其他应用程序访问,二其他应用程序也可以通过ContentProvider对指定的应用程序进行访问。将自己的数据公开给其他应用使用有2种方法,1.定义自己的ContentProvider子类,2.将当前应用的数据添加到已有的ContentProvider中。

下面是一个用ContentProvider访问手机联系人的例子。

MainActivity代码:

public class MainActivity extends Activity {

private String[] columns={Contacts._ID,

Contacts.DISPLAY_NAME,

Phone.NUMBER,

Phone.CONTACT_ID

};

private ListView listview;

private Dialog dialog;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

listview=(ListView)findViewById(R.id.list1);

dialog=new Dialog(MainActivity.this);

dialog.setTitle("电话");

dialog.setContentView(R.layout.content_dialog);

dialog.setCanceledOnTouchOutside(true);

showinfo();

listview.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView arg0, View arg1, int arg2,

long arg3) {

// TODO Auto-generated method stub

dialog.show();

}

});

}

private void showinfo(){

ArrayList> list=new ArrayList>();

ContentResolver resolver=getContentResolver();

Cursor cursor=resolver.query(Contacts.CONTENT_URI, null,null,null,null);

  while(cursor.moveToNext()){

  HashMap map=new HashMap();

  String liststr="";

  int idindex=cursor.getColumnIndex(columns[0]);

  int nameindex=cursor.getColumnIndex(columns[1]);

  int id=cursor.getInt(idindex);

  String name=cursor.getString(nameindex);

  Cursor phone=resolver.query(Phone.CONTENT_URI, null,columns[3]+"="+id,null,null);

  while(phone.moveToNext()){

  int phonenumberindex=phone.getColumnIndex(columns[2]);

  String phonenumber=phone.getString(phonenumberindex);

  liststr+=phonenumber;

  }

  map.put("name", name);

  map.put("phonenumber", liststr);

  list.add(map);

  }

  cursor.close();

  SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.simple_adapter_item, new String[]{"name","phonenumber"}, new int[]{R.id.row_text1,R.id.row_text2});

  listview.setAdapter(simpleAdapter);

}

}

这段主要代码中,Dialog是一个点击联系人即可出现的一个悬浮对话框,即红色部分。

联系人列表主要是showinfo()方法,即蓝色部分,ListView中用的是SimpleAdapter适配。

对应的Xml文件比较简单,在这里也给出:

ContentProvider浅谈+实例

activity_main.xml:

  xmlns:tools="http://schemas.android.com/tools"

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  tools:context=".MainActivity" >

  

    android:id="@+id/list1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:scrollbarAlwaysDrawVerticalTrack="true">

  

content_dialog.xml:

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical" >

  

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="--电话号码--"/>

simple_adapter_item.xml:

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical" >

  

    android:id="@+id/row_text1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="--------"

    />

  

    android:id="@+id/row_text2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="--------"

    />

同时,需要进行权限设置,在AndroidMainfest.xml里,

这样就ok了,可以访问系统的电话簿了。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:ContentProvider浅谈+实例-创新互联
网址分享:http://scyingshan.cn/article/hjcij.html