Android学习笔记之ContentProvider

读取其他应用程序共享的数据

以下示例为读取联系人信息

package com.jiahemeikang.helloandroid;

import com.jiahemikang.service.EchoService;
import com.jiahemikang.service.EchoService.EchoServiceBingder;
import com.jikexuyua.broadcastreceiver.MyBC;

import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,ServiceConnection{

    private TextView tvResult ;
    private Intent serviceIntent;
    private Button btnStarAty1; 
    private Button btnStarService; 
    private Button btnStopService; 
    
    private Button btnBingService; 
    private Button btnUnBingService;
    private Button btnGetNum;
    private Button btnbroadcast;
    private Button regbroadcastButton;
    private Button btnunregbroadcastButton;
    public EchoService echoService = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        serviceIntent = new Intent(this,EchoService.class);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult = (TextView)findViewById(R.id.tvResult);
        
        btnStarAty1= (Button)findViewById(R.id.btnStartAty1);
        btnStarService= (Button)findViewById(R.id.btnStartService);
        btnStopService= (Button)findViewById(R.id.btnStopService);
        
        btnStarService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
        
        
        btnBingService= (Button)findViewById(R.id.bingdingservice);
        btnUnBingService= (Button)findViewById(R.id.unbingdingservice);
        btnBingService.setOnClickListener(this);
        btnUnBingService.setOnClickListener(this);
        
        btnGetNum= (Button)findViewById(R.id.btnGetNum);
        btnGetNum.setOnClickListener(this);
        
        btnbroadcast= (Button)findViewById(R.id.btnbroadcast);
        btnbroadcast.setOnClickListener(this);
        
        regbroadcastButton= (Button)findViewById(R.id.btnregbroadcast);
        regbroadcastButton.setOnClickListener(this);
        btnunregbroadcastButton= (Button)findViewById(R.id.btnunregbroadcast);
        btnunregbroadcastButton.setOnClickListener(this);
        
        btnStarAty1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                
                Intent i = new Intent(MainActivity.this,Aty1.class);
                i.putExtra("txt","Hello aty1");
                startActivity(i);
                startActivityForResult(i,0);
            }
        });
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            String _strName =cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            System.out.println(">>>>>>>>>>>>>>>"+_strName);
        }
    }

    @Override
    protected void onActivityResult(int a,int b ,Intent i){
        
        if (i!=null) {
            String result = i.getStringExtra("result");
            
            tvResult.setText(result);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private  final MyBC myBC = new MyBC();
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnStartService:
            startService(serviceIntent);
            break;
        case R.id.btnStopService:
            stopService(serviceIntent);
            break;
        case R.id.bingdingservice:
            bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
            break;
        case R.id.unbingdingservice:
            unbindService(this);
            echoService =null;
            break;
        case R.id.btnGetNum:
            if (echoService!=null) {
                System.out.print("当前服务的 数字为"+echoService.getCountNum());
            }
            break;
        case R.id.btnbroadcast:
            Intent i = new Intent(MyBC.ACTION);
            i.putExtra("txt", "消息");
            sendBroadcast(i);
            break;
        case R.id.btnregbroadcast:
            registerReceiver(myBC, new IntentFilter(MyBC.ACTION));
            break;
        case R.id.btnunregbroadcast:
            unregisterReceiver(myBC);
            break;
        default:
            break;
        }
    }

    
    @Override
    public void onServiceConnected(ComponentName arg0, IBinder binder) {
        System.out.println("onServiceConnected");
        echoService = ((EchoService.EchoServiceBingder)binder).getService();
        
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub
        
    }
    
    
    
    //onStart

}

 

加入读取联系人权限

 

最后结果

posted @ 2016-03-02 20:53  喜爱糖葫芦  阅读(195)  评论(0编辑  收藏  举报