×

android学习系列-短信发送器与电话拨号器调用(3)

Kalet Kalet 发表于2013-12-20 11:09:32 浏览182 评论0

抢沙发发表评论

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> 

群贤毕至

访客