Android 打开URL中的网页和拨打电话、发送短信功能

时间:2022-11-10 13:24:53

拨打电话需要的权限

<uses-permission android:name="android.permission.CALL_PHONE"/>

 

为了省事界面都写一起了,有点乱

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="打开URL" />

    <EditText
        android:id="@+id/tel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:text="18900008888" />

    <Button
        android:id="@+id/btn02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="拨打电话" />

    <TableLayout
        android:id="@+id/TableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableRow android:id="@+id/TableRow1" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="收信人:" />

            <EditText
                android:id="@+id/msg_tel"
                android:layout_width="260px"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:text="" />
        </TableRow>

        <TableRow android:id="@+id/TableRow2" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="内容:" />

            <EditText
                android:id="@+id/msg_text"
                android:layout_width="260px"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:lines="3" />
        </TableRow>
    </TableLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
           <Button 
                android:id="@+id/btn03"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="发送短信" />
        
    </LinearLayout>
</LinearLayout>

 

MainActivity.java

public class MainActivity extends Activity {
    private Button btn01=null;
    private Button btn02=null;
    private EditText tel=null;
    private EditText msg_tel=null;
    private EditText msg_text=null;
    private Button btn03=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        this.btn01=(Button)super.findViewById(R.id.btn01);
        this.btn01.setOnClickListener(new OnClickListenerImpl());
        
        this.tel=(EditText)super.findViewById(R.id.tel);
        this.btn02=(Button)super.findViewById(R.id.btn02);
        this.btn02.setOnClickListener(new OnClickListenerImpl());
        
        this.msg_tel=(EditText)super.findViewById(R.id.msg_tel);
        this.msg_text=(EditText)super.findViewById(R.id.msg_text);
        this.btn03=(Button)super.findViewById(R.id.btn03);
        this.btn03.setOnClickListener(new OnClickListenerImpl());
    }
    
    private class OnClickListenerImpl implements OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btn01: //打开网页
                Uri uri=Uri.parse("http://www.hrb80.com");
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(uri);
                MainActivity.this.startActivity(intent);
                break;
            case R.id.btn02: //拨打电话
                String telString=MainActivity.this.tel.getText().toString();
                Uri uriTel=Uri.parse("tel:"+telString);
                Intent intentTel=new Intent();
                intentTel.setAction(Intent.ACTION_CALL);
                intentTel.setData(uriTel);
                MainActivity.this.startActivity(intentTel);
                break;
            case R.id.btn03: //发送短信
                String msg_telString=MainActivity.this.msg_tel.getText().toString();
                String msg_textString=MainActivity.this.msg_text.getText().toString();
                Uri uriMsg=Uri.parse("smsto:"+msg_telString);
                Intent intentMsg=new Intent();
                intentMsg.setAction(Intent.ACTION_SENDTO);
                intentMsg.putExtra("sms_body", msg_textString);
                intentMsg.setType("vnd.android-dir/mms-sms"); //短信的MIME类型
                intentMsg.setData(uriMsg);
                MainActivity.this.startActivity(intentMsg);
                break;
            default:
                break;
            }
        }
    }