android開發(fā)基礎(chǔ)教程—打電話發(fā)短信
更新時(shí)間:2013年01月09日 17:33:58 作者:
打電話發(fā)短信的功能已經(jīng)離不開我們的生活了,記下來介紹打電話發(fā)短信的具體實(shí)現(xiàn)代碼,感興趣的朋友可以了解下
打電話發(fā)短信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();
//設(shè)置動(dòng)作名稱
i.setAction("android.intent.action.CALL");
//設(shè)置意圖數(shù)據(jù)
i.setData(Uri.parse("tel:" + phoneNo));
//開始活動(dòng)
startActivity(i);
}
//發(fā)短信
else if(id == R.id.button2){
String smsContent = mMessage.getText().toString();
//得到默認(rèn)的短信管理器
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phoneNo, null, smsContent, null, null);
//提示用戶信息,臨時(shí)性通知
Toast toast = Toast.makeText(MainActivity.this, "發(fā)送完成", 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;
}
}
布局代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClick"
android:text="打電話" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:onClick="onClick"
android:text="發(fā)短信" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:ems="10"
android:hint="請(qǐng)輸入電話號(hào)碼"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:hint="請(qǐng)輸入短信內(nèi)容"
android:ems="10" />
</RelativeLayout>
復(fù)制代碼 代碼如下:
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();
//設(shè)置動(dòng)作名稱
i.setAction("android.intent.action.CALL");
//設(shè)置意圖數(shù)據(jù)
i.setData(Uri.parse("tel:" + phoneNo));
//開始活動(dòng)
startActivity(i);
}
//發(fā)短信
else if(id == R.id.button2){
String smsContent = mMessage.getText().toString();
//得到默認(rèn)的短信管理器
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(phoneNo, null, smsContent, null, null);
//提示用戶信息,臨時(shí)性通知
Toast toast = Toast.makeText(MainActivity.this, "發(fā)送完成", 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;
}
}
布局代碼:
復(fù)制代碼 代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClick"
android:text="打電話" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:onClick="onClick"
android:text="發(fā)短信" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:ems="10"
android:hint="請(qǐng)輸入電話號(hào)碼"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:hint="請(qǐng)輸入短信內(nèi)容"
android:ems="10" />
</RelativeLayout>
相關(guān)文章
Android編程實(shí)現(xiàn)根據(jù)不同日期計(jì)算天數(shù)差的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)根據(jù)不同日期計(jì)算天數(shù)差的方法,涉及Android調(diào)用日期類Calendar實(shí)現(xiàn)時(shí)間運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-03-03詳細(xì)講解Android中使用LoaderManager加載數(shù)據(jù)的方法
這篇文章主要介紹了Android中使用LoaderManager加載數(shù)據(jù)的方法,講到了LoaderManager的異步加載與聲明周期的管理等相關(guān)用法,需要的朋友可以參考下2016-04-04Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼
這篇文章主要介紹了Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,代碼中包含注釋,需要的朋友可以參考下2015-04-04Android編程實(shí)現(xiàn)獲取圖片資源的四種方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取圖片資源的四種方法,分別針對(duì)圖片所在目錄位置分析了Android獲取圖片資源的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11