android使用Intent操作拨打号码发送短信
Activity程序Activity.java
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;
}
}
界面配置文件,采用线性布局内嵌表格布局activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="号码:"/>
<EditText
android:id="@+id/tel"
android:layout_width="260px"
android:layout_height="wrap_content"
android:numeric="integer"/>
<Button
android:id="@+id/addbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="内容:"/>
<EditText
android:id="@+id/content"
android:layout_width="260px"
android:layout_height="wrap_content"
android:gravity="top"
android:lines="6"/>
</TableRow>
</TableLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信" />
<Button
android:id="@+id/telbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话 "/>
</LinearLayout>
</LinearLayout>
权限设置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentcaseproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>
文章来源: liyuanjinglyj.blog.csdn.net,作者:李元静,版权归原作者所有,如需转载,请联系作者。
原文链接:liyuanjinglyj.blog.csdn.net/article/details/9383487
- 点赞
- 收藏
- 关注作者
评论(0)