一、电话拨号
main.xml代码:
<EditText android:id="@+id/setValues" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电话拨号" android:onClick="testOne" />
MainActivity.java代码:
private EditText setValues; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setValues=(EditText) findViewById(R.id.setValues); } //电话拨号 public void testOne(View view){ //ACTION_DIAL:调出拨号界面 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+setValues.getText().toString())); startActivity(intent); }
如果只是按照上面的代码来做,一定会报错,为什么呢?看图
原来是还没有给权限,看来做开发不能马虎啊!!!
这时你只需要在AndroidManifest.xml中给这个电话拨号需要的组件权限就OK啦
AndroidManifest.xml代码:
<!-- 电话拨号权限 --> <uses-permission android:name="android.permission.CALL_PHONE"/>
二、发送短信息
第一种:
main.xml代码:
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送短信" android:onClick="testTwo" />
MainActivity.java代码:
//发送短信息 public void testTwo(View view){ Intent intent=new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+setValues.getText().toString())); intent.putExtra("sms_body", "Hello,Android"); startActivity(intent); }
同样,发送短信也是需要相应的权限
AndroidManifest.xml代码:
<uses-permission android:name="android.permission.SEND_SMS"/>
第二种:
main.xml代码:
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送短信2" android:onClick="testTwo_2" />
MainActivity.java代码:
//发信息 //一次性发十条信息 public void testTwo_2(View view){ SmsManager sms=SmsManager.getDefault(); for (int i = 0; i < 10; i++) { sms.sendTextMessage("smsto:"+setValues.getText().toString(), null, "this is a test!", null, null); } }
和上面的第一种一样,需要给权限