android案例之傳送簡訊

來源:互聯網
上載者:User

其實傳送簡訊的步驟和打電話的步驟是相同的 
步驟一:頁面 
1、  res/layout/main.xml檔案 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/user" /> 
    <EditText 
        android:id="@+id/user" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" /> 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/content" /> 
    <EditText 
        android:id="@+id/content" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" /> 
   <Button 
        android:id="@+id/send" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/send" /> 
2、  res/values/strings.xml 
    <string name="app_name">傳送簡訊介面</string> 
    <!-- 第一步:寫出相應的常值內容 --> 
    <string name="user">接受者</string> 
<string name="content">資訊內容</string> 
<string name="send">發送</string> 
步驟二:必要的配置 
需要注意的是發簡訊的意圖與打電話的意圖是不同的 
SendMessageActivity .java 
public class SendMessageActivity extends Activity implements OnClickListener{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        //擷取當前視圖下面的組件 
        Button sendBtn=(Button) findViewById(R.id.send); 
        //註冊事件 
        sendBtn.setOnClickListener(this); 
    } 
    @SuppressWarnings("deprecation") 
    @Override 
    public void onClick(View v) { 
        // TODO Auto-generated method stub 
        EditText user=(EditText) findViewById(R.id.user); 
        EditText content=(EditText) findViewById(R.id.content); 
        //資訊管理對象 
        SmsManager smsManager=SmsManager.getDefault(); 
        //意圖      後三個參數所代表的     請求碼      普通的意圖       狀態 
        PendingIntent intent=PendingIntent.getBroadcast(SendMessageActivity.this, 0, new Intent(), 0); 
        //發送資訊 
        smsManager.sendTextMessage(user.getText().toString(), null, content.getText().toString(), 
                intent, null); 
        //提示資訊發送成功 
        Toast.makeText(SendMessageActivity.this, "資訊發送成功", Toast.LENGTH_LONG).show(); 
    } 

步驟三:添加使用者權限 
與打電話時步驟相似,只不過許可權是android.permission.SEND_SMS 

 


作者 w_l_j

聯繫我們

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