android(4)_發簡訊操作

來源:互聯網
上載者:User

strings.xml

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
    <string name="app_name">lession01_sms</string> 
    <string name="action_settings">Settings</string> 
    <string name="phone_num">請輸入手機號碼</string> 
    <string name="phone_content">請輸入發送內容</string> 
    <string name="phone_button">傳送簡訊</string> 
 
</resources> 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">lession01_sms</string>
    <string name="action_settings">Settings</string>
    <string name="phone_num">請輸入手機號碼</string>
    <string name="phone_content">請輸入發送內容</string>
    <string name="phone_button">傳送簡訊</string>

</resources>

 


布局檔案:activity_sms.xml

[html] 
<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".SmsActivity" > 
 
    <TextView 
        android:id="@+id/view_phone" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/phone_num" /> 
    <EditText 
         android:id="@+id/edit_num" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="phone" 
        android:layout_below="@+id/view_phone" 
        android:layout_alignParentLeft="true" /> 
    <TextView 
        android:id="@+id/view_content" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/phone_content" 
        android:layout_below="@+id/edit_num" /> 
    <EditText 
         android:id="@+id/edit_content" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="text" 
        android:minLines="5" 
        android:layout_below="@+id/view_content"/> 
    <Button 
        android:id="@+id/button_send" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/phone_button" 
        android:layout_below="@+id/edit_content"/> 
 
</RelativeLayout> 

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SmsActivity" >

    <TextView
        android:id="@+id/view_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_num" />
    <EditText
         android:id="@+id/edit_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:layout_below="@+id/view_phone"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/view_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_content"
        android:layout_below="@+id/edit_num" />
    <EditText
         android:id="@+id/edit_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:minLines="5"
        android:layout_below="@+id/view_content"/>
    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_button"
        android:layout_below="@+id/edit_content"/>

</RelativeLayout>
 

 


添加許可權

 

 

SmsActivity.java

[java]
package com.example.lession01_sms; 
 
import java.util.ArrayList; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.telephony.SmsManager; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class SmsActivity extends Activity { 
 
    // 聲明控制項對象  
    public Button btn_send; 
    public EditText edit_num; 
    public EditText edit_content; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        // 設定顯示的視圖(布局檔案)  
        setContentView(R.layout.activity_sms); 
        // 擷取控制項對象  
        btn_send = (Button) findViewById(R.id.button_send); 
        edit_num = (EditText) findViewById(R.id.edit_num); 
        edit_content = (EditText) findViewById(R.id.edit_content); 
 
        // 為按鈕控制項註冊事件  
        btn_send.setOnClickListener(new View.OnClickListener() { 
 
            @Override 
            public void onClick(View arg0) { 
                // 擷取電話號碼  
                String phone_num = edit_num.getText().toString(); 
                // 擷取發送內容  
                String phone_content = edit_content.getText().toString(); 
                // 擷取簡訊管理器對象  
                SmsManager smsManager = SmsManager.getDefault(); 
                // 意圖對象  
                PendingIntent pendingIntent = PendingIntent.getBroadcast( 
                        SmsActivity.this, 0, new Intent(), 0); 
 
                //70分頁  
                ArrayList<String> contents = smsManager.divideMessage(phone_content); 
                for(String c :contents){ 
                    smsManager.sendTextMessage(phone_num, null, c, pendingIntent, null); 
                } 
                 
                 
                // 發送資訊操作  
                smsManager.sendTextMessage(phone_num, null, phone_content, 
                        pendingIntent, null); 
                // 多士效果  
                Toast.makeText(SmsActivity.this, "發送完畢", Toast.LENGTH_LONG) 
                        .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.sms, menu); 
        return true; 
    } 
 

package com.example.lession01_sms;

import java.util.ArrayList;

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

public class SmsActivity extends Activity {

 // 聲明控制項對象
 public Button btn_send;
 public EditText edit_num;
 public EditText edit_content;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // 設定顯示的視圖(布局檔案)
  setContentView(R.layout.activity_sms);
  // 擷取控制項對象
  btn_send = (Button) findViewById(R.id.button_send);
  edit_num = (EditText) findViewById(R.id.edit_num);
  edit_content = (EditText) findViewById(R.id.edit_content);

  // 為按鈕控制項註冊事件
  btn_send.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View arg0) {
    // 擷取電話號碼
    String phone_num = edit_num.getText().toString();
    // 擷取發送內容
    String phone_content = edit_content.getText().toString();
    // 擷取簡訊管理器對象
    SmsManager smsManager = SmsManager.getDefault();
    // 意圖對象
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      SmsActivity.this, 0, new Intent(), 0);

    //70分頁
    ArrayList<String> contents = smsManager.divideMessage(phone_content);
    for(String c :contents){
     smsManager.sendTextMessage(phone_num, null, c, pendingIntent, null);
    }
    
    
    // 發送資訊操作
    smsManager.sendTextMessage(phone_num, null, phone_content,
      pendingIntent, null);
    // 多士效果
    Toast.makeText(SmsActivity.this, "發送完畢", Toast.LENGTH_LONG)
      .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.sms, menu);
  return true;
 }

}

 
 

 

 

 



 

 

 

 

 

 

 

 

 

 

 


 

相關文章

聯繫我們

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