Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

基於 Android 的撥打電話與發送短信實例介紹

編輯:關於 Android 程式設計

本節透過 Intent 機制與 SmsManager,介紹如何在 Android 中實現撥打電話與發送短信這兩個常見功能。

Intent:當找到與設置的 Intent 限制相同的 Activity 時,透過 startActivity() 就會啟動該 Activity。

電話撥號器:

intent.setAction("android.intent.action.CALL");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:" + strmobile));
startActivity(intent);//方法內部會自動為 Intent 添加類別:android.intent.category.DEFAULT

短信發送器:

SmsManager manager = SmsManager.getDefault();
ArrayList<String> texts = manager.divideMessage(comtent);
for (String text : texts) {
manager.sendTextMessage(number, null, text, null, null);// 4和5參數分別為短信發送狀態,對方是否收到短信狀態;
}
Toast.makeText(getApplicationContext(),R.string.success,Toast.LENGTH_LONG).show();
Toast 對話框 // Toast.makeText(MainActivity.this, resId, duration);//內部類訪問外部類;

****************************************************************

findViewById(R.id.button);// 根據 id 查找顯示控件;
View.OnClickListener() {
@Override
public void onClick(View arg0) {
//arg0 就是被點擊的對象
}
}

@+id/button:在 R 文件的 id 內部類裡面添加一個 id 為 button 的常量,用該常量的值作為此控件的 id 值;
@android:表示訪問 android 包下的 R 文件;
@id/label:表示訪問 id 為 label 的文件(相對布局)。

單元測試:
繼承類:AndroidTestCase
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.hellokity" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>

actual = function();
Assert.assertEquals(3,actual);//判斷 actual 是否為 3。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved