当前位置: 编程技术>移动开发
android开发基础教程—打电话发短信
来源: 互联网 发布时间:2014-10-12
本文导语: 打电话发短信demo 代码如下: public class MainActivity extends Activity { EditText mPhoneNum,mMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPhoneNum=(EditText) findViewById(R.id.editTex...
打电话发短信demo
public class MainActivity extends Activity {
EditText mPhoneNum,mMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPhoneNum=(EditText) findViewById(R.id.editText1);
mMessage=(EditText) findViewById(R.id.editText2);
}
public void onClick(View v) {
int id = v.getId();
String phoneNo = mPhoneNum.getText().toString();
//呼叫
if(id == R.id.button1){
Intent i = new Intent();
//设置动作名称
i.setAction("android.intent.action.CALL");
//设置意图数据
i.setData(Uri.parse("tel:" + phoneNo));
//开始活动
startActivity(i);
}
//发短信
else if(id == R.id.button2){
String smsContent = mMessage.getText().toString();
//得到默认的短信管理器
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phoneNo, null, smsContent, null, null);
//提示用户信息,临时性通知
Toast toast = Toast.makeText(MainActivity.this, "发送完成", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 50, 100);
toast.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局代码:
代码如下:
public class MainActivity extends Activity {
EditText mPhoneNum,mMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPhoneNum=(EditText) findViewById(R.id.editText1);
mMessage=(EditText) findViewById(R.id.editText2);
}
public void onClick(View v) {
int id = v.getId();
String phoneNo = mPhoneNum.getText().toString();
//呼叫
if(id == R.id.button1){
Intent i = new Intent();
//设置动作名称
i.setAction("android.intent.action.CALL");
//设置意图数据
i.setData(Uri.parse("tel:" + phoneNo));
//开始活动
startActivity(i);
}
//发短信
else if(id == R.id.button2){
String smsContent = mMessage.getText().toString();
//得到默认的短信管理器
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phoneNo, null, smsContent, null, null);
//提示用户信息,临时性通知
Toast toast = Toast.makeText(MainActivity.this, "发送完成", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.RIGHT, 50, 100);
toast.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局代码:
代码如下: