1. 程式人生 > >安卓案例:短信發送器

安卓案例:短信發送器

trim intent lar avi tostring iss too appt extend

效果:

技術分享圖片

幾個簡單的布局:

main

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <
RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入手機號碼:"
/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/et_number" android:layout_alignParentRight="true" android:onClick="click" android:text="+" /> </
RelativeLayout> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="200dp" android:gravity="top" android:hint="請輸入短信內容:" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="insert" android:text="插入短信模板" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="send" android:text="發送" /> </LinearLayout>

聯系人布局contact:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

模板布局template:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ListView
        android:id="@+id/lv" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        ></ListView>

</LinearLayout>

contact_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp" />

</LinearLayout>

template_item:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#000000" />

MainActivity:

package org.dreamtech.smsmg;

import java.util.ArrayList;

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

public class MainActivity extends Activity {

    private EditText et_content;
    private EditText et_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_content = (EditText) findViewById(R.id.et_content);
        et_number = (EditText) findViewById(R.id.et_number);
    }

    // 顯示意圖
    public void click(View v) {
        Intent intent = new Intent(this, ContactActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == 10) {
            String phone = data.getStringExtra("phone");
            et_number.setText(phone);
        } else if (resultCode == 20) {
            String smsContent = data.getStringExtra("smsContent");
            et_content.setText(smsContent);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    // 顯示意圖
    public void insert(View v) {
        Intent intent = new Intent(this, SmsTemplateActivity.class);
        startActivityForResult(intent, 2);
    }

    // 隱式意圖
    public void send(View v) {
        String number = et_number.getText().toString().trim();
        String content = et_content.getText().toString().trim();
        SmsManager smsManager = SmsManager.getDefault();
        ArrayList<String> divideMessages = smsManager.divideMessage(content);
        for (String div : divideMessages) {
            smsManager.sendTextMessage(number, null, div, null, null);
        }
    }
}

ContactActivity:

package org.dreamtech.smsmg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ContactActivity extends Activity {
    private List<Person> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        ListView lv = (ListView) findViewById(R.id.lv);

        lists = new ArrayList<Person>();

        for (int i = 0; i < 20; i++) {
            Person p = new Person();
            p.setName("張" + i);
            p.setPhone("11" + i);
            lists.add(p);
        }
        lv.setAdapter(new MyAdapter());
        
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String phone = lists.get(position).getPhone();
                
                Intent intent = new Intent();
                intent.putExtra("phone", phone);
                
                setResult(10, intent);
                
                finish();
                
            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view;
            if (convertView == null) {
                view = View.inflate(getApplicationContext(),
                        R.layout.contact_item, null);
            } else {
                view = convertView;
            }
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
            tv_name.setText(lists.get(position).getName());
            tv_phone.setText(lists.get(position).getPhone());
            return view;
        }

    }
}

SmsTemplateActivity:

package org.dreamtech.smsmg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SmsTemplateActivity extends Activity {

    String objects[] = { "我在吃飯,請稍後聯系", "我在開會,請稍後聯系", "我在上課,請稍後聯系",
            "我在打代碼,請稍後聯系", "我在約會,請稍後聯系" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template);

        ListView lv = (ListView) findViewById(R.id.lv);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(), R.layout.smstemplate_item, objects);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String smsContent = objects[position];

                Intent intent = new Intent();
                intent.putExtra("smsContent", smsContent);
                setResult(20, intent);
                finish();
            }
        });
    }
}

Person類:

package org.dreamtech.smsmg;

public class Person {
    private String name;
    private String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

註冊Activity和配置權限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.dreamtech.smsmg"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.dreamtech.smsmg.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>
        <activity android:name="org.dreamtech.smsmg.ContactActivity" >
        </activity>
        <activity android:name="org.dreamtech.smsmg.SmsTemplateActivity" >
        </activity>
    </application>

</manifest>

安卓案例:短信發送器