打电话/发短信

1.知识点
打电话和发短信:
a. 进入电话拨号界面的意图:Intent.ACTION_DIAL
b. 进入短信编辑界面的意图:Intent.ACTION_SENDTO
c. 拨打电话的意图:Itent.ACTION_CALL
d. 发送短信的工具类:SmaManager
e. 需要的权限:
    a. 打电话的权限:android.permission.CALL_PHONE
    b. 发短信的权限:android.permission.SEND_SMS
f. 拨打电话的Uri: intent.setData(Uri.parse("tel:" + number));
g. 进入短息界面的电话Uri: intent.setData(Uri.parse("smsto" + number));
h. 携带额外短信数据的标识名为:sms_body


功能描述:


1).点击“打电话”:进入比好界面
2).长按“打电话”:进入拨号界面
3).点击“发送短信”:进入编辑短信界面
4).长按“发送短信”:直接将短信发送出去


. 技术点
1).布局的设置
2).点击事件和长按事件监听的添加
3).使用隐士意图拨打电话,进入拨号界面,进入发短信界面
4).使用SMSMessager发送短信
5).权限的声明(如打电话,发短信)


具体步骤:

1. 界面布局
    1).分析 垂直的LinearLayout,里面是一个水平的LinearLayout
    2).编码
2. 在Activity中初始化需要操作的视图对象
3. 给Button设置点击监听
4. 给Button设置长按监听
5. 点击打电话进入拨号界面


    1).创建一个Intent(隐式)
    2).携带数据
    3).startActivity()


6. 长按打电话进入打电话界面


    1).创建一个Intent(隐式)
    2).携带数据
    3).startActivity()


7. 点击打发短信进入打短信编辑界面


    1).创建一个Intent(隐式)
    2).携带数据(号码/内容)
    3).startActivity(intent)


8. 长按发短信直接发短信
    1). 得到SmsManager的对象
    2). 发送文本信息(短信)
    3).


Activity代码实现:


public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {


    private EditText et_main_call;
    private EditText et_main_send;
    private Button bt_main_call;

    private Button bt_main_send;


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if(v == bt_main_call){//打电话

//                Toast.makeText(MainActivity.this,"点击打电话",0).show();


//                1).创建一个Intent(隐式)
                String action = "android.intent.action.DIAL";
                action = Intent.ACTION_DIAL;
                Intent intent = new Intent(action);


//                2).携带数据
                String number =  et_main_call.getText().toString();
                intent.setData(Uri.parse("tel:" + number));
//                3).startActivity()
                startActivity(intent);


            }else if(v == bt_main_send){//发短信
//                Toast.makeText(MainActivity.this,"点击发短信",0).show();
//                1).创建一个Intent(隐式)
                Intent intent = new Intent(Intent.ACTION_SENDTO);
//                2).携带数据(号码/内容)
                String number = et_main_call.getText().toString();
                String sms = et_main_send.getText().toString();


                intent.setData(Uri.parse("smsto:" + number));
                intent.putExtra("sms_body",sms);


//                3).startActivity(intent)
                startActivity(intent);


            }


        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //初始化视图对象
        et_main_call = (EditText) findViewById(R.id.et_main_call);
        et_main_send = (EditText) findViewById(R.id.et_main_send);
        bt_main_call = (Button) findViewById(R.id.bt_main_call);
        bt_main_send = (Button) findViewById(R.id.bt_main_send);


        //给视图对象设置点击监听
        bt_main_call.setOnClickListener(onClickListener);
        bt_main_send.setOnClickListener(onClickListener);


        //给视图对象设置长点击监听
        bt_main_call.setOnLongClickListener(this);
        bt_main_send.setOnLongClickListener(this);




    }


    @Override
    public boolean onLongClick(View v) {


        if(v == bt_main_call){//打电话
//            Toast.makeText(MainActivity.this,"长按打电话",0).show();
//            1).创建一个Intent(隐式)
            Intent intent = new Intent(Intent.ACTION_CALL); //String ACTION_CALL = "android.intent.action.CALL";
//            2).携带数据
            String number = et_main_call.getText().toString();
            intent.setData(Uri.parse("tel:" + number));
//            3).startActivity()
            startActivity(intent);


        }else if(v == bt_main_send){//发短信
//            Toast.makeText(MainActivity.this,"长按发短信",0).show();


//            1). 得到SmsManager的对象
            SmsManager smsManager = SmsManager.getDefault();
//            2). 发送文本信息(短信)
            String number = et_main_call.getText().toString();
            String sms = et_main_send.getText().toString();
            smsManager.sendTextMessage(number,null,sms,null,null);


        }
        return true;//如果用默认的false则长点击之后还会触发点击事件
        //为true表示此事件已经被消费了,不会再触发点击事件
    }
}




XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.callandsendmessage.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话号码:" />


        <EditText
        android:id="@+id/et_main_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="请输入号码" />


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


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


        <EditText
            android:id="@+id/et_main_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="请输入短信" />


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">


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


        <Button
            android:id="@+id/bt_main_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发短信" />


    </LinearLayout>
</LinearLayout>




功能清单文件:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.callandsendmessage">


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值