android開發學習:打電話和發簡訊,android發簡訊

來源:互聯網
上載者:User

android開發學習:打電話和發簡訊,android發簡訊

1.建立一個android項目

File——New——Other——android application project

填寫application name(就是應用的名字,比如:天天酷跑)

填寫project name(就是程式項目名,比如:TTKP,打包後名字也是TTKP.APP)

填寫package name(程式包名,比如cn.tengxun.ttkp)

然後選擇最小啟動並執行android版本,最適合版本,編譯版本,主題。

NEXT——NEXT——選擇你android應用表徵圖圖片,然後完成。


然後我們要關注的res(放資源檔的,靜態文字可以寫在裡面)

src代碼編程檔案

gen(自動產生的資源ID組建檔案)

AndroidManifest.xml是應用設定檔

res下的layout是布局的設定檔


2.編寫一個打電話功能

先編寫布局設定檔activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"      android:layout_width="match_parent"     android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/phone_title" />    <EditText         android:layout_width="match_parent"        android:layout_height="wrap_content"          android:hint="@string/phone_title"        android:id="@+id/telnum" />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_button"         android:id="@+id/button"/></LinearLayout>

@的意思是操作gen下面的R.java檔案的資訊,擷取;@+是建立。


編寫一下文本的資訊

res下面的value下的string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">撥號器</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>    <string name="phone_title">請輸入手機號</string>    <string name="phone_button">撥號</string></resources>

編寫撥號事件代碼

在src下的java代碼

public class MainActivity extends Activity {    private EditText edittext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        edittext=(EditText) findViewById(R.id.telnum);        Button button=(Button) this.findViewById(R.id.button);        button.setOnClickListener(new ButtonClickListener());    }        private final class ButtonClickListener implements View.OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString telnum=edittext.getText().toString();Intent intent=new Intent();intent.setAction("android.intent.action.CALL");intent.setData(Uri.parse("tel:"+telnum));startActivity(intent);}        }}

最後你要擷取你調用android打電話這個功能的許可權

在AndroidManifest.xml應用設定檔

<uses-permission android:name="android.permission.CALL_PHONE" />

3.編寫發簡訊功能

布局設定檔

   <EditText         android:layout_width="match_parent"        android:layout_height="wrap_content"          android:hint="@string/phone_title"        android:id="@+id/telnum" />    <EditText         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:minLines="3"        android:hint="@null"        android:id="@+id/message"                 />       <Button         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/message_button"         android:id="@+id/message_button"/>

string設定檔

<string name="message_button">傳送簡訊</string>

java代碼

public class MainActivity extends Activity {private EditText phonetext;private EditText edittext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        phonetext=(EditText) findViewById(R.id.telnum);        edittext=(EditText) findViewById(R.id.message);        Button message_button=(Button) this.findViewById(R.id.message_button);        message_button.setOnClickListener(new MessageButtonClickListener());            }    private final class MessageButtonClickListener implements View.OnClickListener{@Overridepublic void onClick(View v) {String phoneNumber=phonetext.getText().toString();String message=edittext.getText().toString();SmsManager manager=SmsManager.getDefault();ArrayList<String> messages = manager.divideMessage(message);for(String content:messages){                            //發簡訊                        manager.sendTextMessage(phoneNumber, null, content, null, null);                    //寫入簡訊記錄                        ContentValues values = new ContentValues();                          values.put("address", phoneNumber);                          values.put("body", message);                          values.put("type", "2");                          values.put("read", "1");//1表示已讀                          getContentResolver().insert(Uri.parse("content://sms/inbox"), values);                          }                 Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();                   }          } }

加入許可權

    <uses-permission android:name="android.permission.SEND_SMS" />    <uses-permission android:name="android.permission.WRITE_SMS"/>      <uses-permission android:name="android.permission.READ_SMS"/>

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.