java发短信 打电话_发短信和打电话

packagecom.example.call_msm;importandroidx.annotation.RequiresApi;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.app.ActivityCompat;importandroid.Manifest;importandroid.annotation.SuppressLint;importandroid.content.Intent;importandroid.content.pm.PackageManager;importandroid.net.Uri;importandroid.os.Build;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;import static android.Manifest.*;public class MainActivity extendsAppCompatActivityimplementsView.OnLongClickListener {privateButton btn_call;privateButton btn_sms;privateEditText editText_number;privateEditText editText_message;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn_call=findViewById(R.id.btn_call);

btn_sms=findViewById(R.id.btn_sms);

btn_call.setOnLongClickListener(this);

btn_sms.setOnLongClickListener(this);

editText_number=findViewById(R.id.editText_number);

editText_message=findViewById(R.id.editText_message);

}

@RequiresApi(api=Build.VERSION_CODES.M)

@Overridepublic booleanonLongClick(View v) {if(v.equals(btn_call)) {

makeCall();

}else if(v.equals(btn_sms)) {

sendMessage();

}return true;

}

@RequiresApi(api=Build.VERSION_CODES.M)private voidsendMessage() {if (checkSelfPermission(permission.SEND_SMS) !=PackageManager.PERMISSION_GRANTED) {//TODO: Consider calling//Activity#requestPermissions//here to request the missing permissions, and then overriding//public void onRequestPermissionsResult(int requestCode, String[] permissions,//int[] grantResults)//to handle the case where the user grants the permission. See the documentation//for Activity#requestPermissions for more details.

ActivityCompat.requestPermissions(this, new String[]{permission.SEND_SMS}, 1);//ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);

return;

}

Toast.makeText(this, "长按发短信", Toast.LENGTH_SHORT).show();

String phoneNumber=editText_number.getText().toString();

Uri uri= Uri.parse("smsto:" +phoneNumber);

Intent intent= newIntent(Intent.ACTION_SEND, uri);

String msg=editText_message.getText().toString();

intent.putExtra("sms_body", msg);

startActivity(intent);

}

@RequiresApi(api=Build.VERSION_CODES.M)private voidmakeCall() {

Toast.makeText(this, "长按打电话", Toast.LENGTH_SHORT).show();

String phoneNumber=editText_number.getText().toString();

Uri uri= Uri.parse("tel:" +phoneNumber);

Intent intent= newIntent(Intent.ACTION_CALL, uri);if (checkSelfPermission(permission.CALL_PHONE) !=PackageManager.PERMISSION_GRANTED) {//TODO: Consider calling//Activity#requestPermissions//here to request the missing permissions, and then overriding//public void onRequestPermissionsResult(int requestCode, String[] permissions,//int[] grantResults)//to handle the case where the user grants the permission. See the documentation//for Activity#requestPermissions for more details.

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},1);//ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);

return;

}

startActivity(intent);

}public voidjumpCall(View view) {

Toast.makeText(this,"打电话",Toast.LENGTH_SHORT).show();

String phoneNumber=editText_number.getText().toString();

Uri uri= Uri.parse("tel:"+phoneNumber);

Intent intent= newIntent(Intent.ACTION_DIAL, uri);

startActivity(intent);

}public voidjumpSendMessage(View view) {

Toast.makeText(this,"发短信",Toast.LENGTH_SHORT).show();

String phoneNumber=editText_number.getText().toString();

Uri uri= Uri.parse("smsto:" +phoneNumber);

Intent intent= newIntent(Intent.ACTION_SENDTO,uri);

String msg=editText_message.getText().toString();

intent.putExtra("sms_body",msg);

startActivity(intent);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值