电话程序:
/phone/src/cn/itcast/phone/MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
EditText mobileText = (EditText) findViewById(R.id.mobile);
String number = mobileText.getText().toString();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
//intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
}
}
/phone/res/layout/main.xml 控制布局并定义元件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/><!-- 添加一个常量 -->
</LinearLayout>
/phone/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">电话拨号器</string>
<string name="mobile">请输入手机号</string>
<string name="button">请拨号</string>
</resources>
/phone/AndroidManifest.xml
<!-- 添加拨打电话权限 系统安装的时候就会提示 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>"
短信发送程序:
public class SmsActivity extends Activity {
private EditText numberText;
private EditText contentText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
numberText = (EditText) this.findViewById(R.id.number_text);
contentText = (EditText) this.findViewById(R.id.content_text);
Button sendButton = (Button) this.findViewById(R.id.send_button);
sendButton.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
String number = numberText.getText().toString();
String content = contentText.getText().toString();
SmsManager manager = SmsManager.getDefault();
ArrayList<String> texts = manager.divideMessage(content);
// 过大则拆分
for (String text : texts) {
manager.sendTextMessage(number, null, text, null, null);
}
Toast.makeText(getApplicationContext(), R.string.send_sucuess, 3).show();
}
}
}
<uses-permission android:name="android.permission.SEND_SMS"/>