package com.example.intentcaseproject;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mybut=null;
private EditText tel=null;
private EditText content=null;
private Button addbut=null;
private Button telbut=null;
private static final int PICK_CONTEACT_SUBACTIVITY=1;//自定义操作标记
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.mybut=(Button)super.findViewById(R.id.mybut);//取得组件
this.tel=(EditText)super.findViewById(R.id.tel);//取得组件
this.content=(EditText)super.findViewById(R.id.content);//取得组件
this.addbut=(Button)super.findViewById(R.id.addbut);//取得组件
this.telbut=(Button)super.findViewById(R.id.telbut);//取得组件
this.mybut.setOnClickListener(new OnClickListenerlmpl());//设置发送短信按钮事件
this.addbut.setOnClickListener(new ContactsOnClickListener());//设置查找联系按钮事件
this.telbut.setOnClickListener(new TelClickListener());// 设置拨打电话号码事件
}
private class TelClickListener implements OnClickListener{//拨号按钮
@Override
public void onClick(View v) {
String telStr=MainActivity.this.tel.getText().toString();//获取电话号码
Uri uri=Uri.parse("tel:"+telStr); //拨打电话号码的URI格式
Intent it=new Intent(); //实例化Intent
it.setAction(Intent.ACTION_CALL); //指定Action
it.setData(uri); //设置数据
MainActivity.this.startActivity(it);//启动Acitivity
}
}
private class ContactsOnClickListener implements OnClickListener{//联系人查找按钮+
@Override
public void onClick(View arg0) {
Uri uri=Uri.parse("content://contacts/people");//连接URi
Intent it=new Intent(Intent.ACTION_PICK,uri);//指定Intent
MainActivity.this.startActivityForResult(it, PICK_CONTEACT_SUBACTIVITY);//调用Intent
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PICK_CONTEACT_SUBACTIVITY://接受返回数据
Uri ret=data.getData();//单个数据的URI
String selection=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?";//查询条件
String[] selectionArgs={String.valueOf(ContentUris.parseId(ret))};//查询参数
Cursor result=super.managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, selection, selectionArgs, null);//查询该联系人的全部号码
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){//循环取出号码
MainActivity.this.tel.setText(//设置到文本框中
result.getString(
result.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
break;
}
}
private class OnClickListenerlmpl implements OnClickListener{//发送短信按钮
@Override
public void onClick(View v) {
String telStr=MainActivity.this.tel.getText().toString();// 取出电话号码
String note=MainActivity.this.content.getText().toString();//取出短信内容
Uri uri=Uri.parse("smsto:"+telStr); //指定路径
Intent it=new Intent(); //实例化Intent
it.setAction(Intent.ACTION_SENDTO); //指定Action
it.putExtra("sms_body", note);
it.setType("vnd.android-dir/mms-sms");
it.setData(uri); //设置数据
MainActivity.this.startActivity(it); //启动Activity
}
}
@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;
}
}