android學習系列-簡訊發送器與電話撥號器調用(3)

來源:互聯網
上載者:User

android學習系列-簡訊發送器與電話撥號器調用(3)

一.簡訊發送器
1)步驟:(1)輸入號碼(2)輸入簡訊內容(3)點擊發送按鈕傳送簡訊。
2)注意事項,簡訊內容太長時,分多條發送。發送完畢提示。
3)後台代碼

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText numberText;
    private EditText contentText;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        numberText = (EditText) this.findViewById(R.id.number);
        contentText = (EditText) this.findViewById(R.id.content);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());//點擊發送按鈕
    }
   
    private final class ButtonClickListener implements View.OnClickListener{

  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(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();//發送成功多士提示。
  }
     
    }
}
4)前台代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
    >
     <TextView 
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="@string/number"
     android:id="@+id/numberlabel"
     />
    
     <EditText
      android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/number"
     android:layout_toRightOf="@id/numberlabel"
     android:layout_alignTop="@id/numberlabel"
     android:layout_marginLeft="5dp"
     />
   
    </RelativeLayout>
 
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/content"
    />
   
    <EditText
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minLines="3"
    android:id="@+id/content"
    />
   
    <Button
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button"
    android:id="@+id/button"
    />
</LinearLayout>
5)注意AndroidManifest.xml檔案中加入傳送簡訊功能許可權配置
<uses-permission android:name="android.permission.SEND_SMS"/>
二,電話撥號器
1)後台代碼

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText mobileText;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mobileText = (EditText) findViewById(R.id.mobile);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
    }
 
    private final class ButtonClickListener implements View.OnClickListener{
  public void onClick(View v) {
   String number = mobileText.getText().toString();
   Intent intent = new Intent();
   intent.setAction("android.intent.action.CALL");
   intent.setData(Uri.parse("tel:"+ number));
   startActivity(intent);//方法內部會自動為Intent添加類別:android.intent.category.DEFAULT
  }
    }
}
2)前台代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
    >
     <TextView 
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="@string/number"
     android:id="@+id/numberlabel"
     />
    
     <EditText
      android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/number"
     android:layout_toRightOf="@id/numberlabel"
     android:layout_alignTop="@id/numberlabel"
     android:layout_marginLeft="5dp"
     />
   
    </RelativeLayout>
 
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/content"
    />
   
    <EditText    
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minLines="3"
    android:id="@+id/content"
    />
   
    <Button
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button"
    android:id="@+id/button"
    />
</LinearLayout>
3)注意AndroidManifest.xml中配置許可權
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="cn.itcast.phone"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".MainActivity"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

    <uses-sdk android:minSdkVersion="8" />

    <--配置許可權-->
    <uses-permission android:name="android.permission.CALL_PHONE"/>

</manifest>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.