Android 新开启Activity返回结果(发送短信应用)

界面:

Android 新开启Activity返回结果(发送短信应用) Android 新开启Activity返回结果(发送短信应用) Android 新开启Activity返回结果(发送短信应用)

界面XML:

主界面:

<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:layout_marginTop="15dp"
            android:hint="请输入或者选择联系人"
            android:inputType="number" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:hint="点击选择"
            android:onClick="selectContact" />
    </RelativeLayout>

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入短信内容"
        android:inputType="textMultiLine"
        android:minLines="5" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="发送"
        android:onClick="sendSms" />

</LinearLayout>

联系人选择界面:

<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=".SelectContactActivity" >

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

</RelativeLayout>

 

<?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" >

    <TextView
        android:id="@+id/tv_contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="姓名:"
        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_contact_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="电话:"
        android:textColor="#99000000"
        android:textSize="17sp" />

</LinearLayout>

主AC代码:

package com.example.sms.sender.pro;

import java.util.ArrayList;

import com.contact.utils.ContactModel;

import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity
{

	private EditText et_phone;
	private EditText et_sms;
	private ContactModel model=null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_phone=(EditText) findViewById(R.id.et_number);
		et_sms=(EditText)findViewById(R.id.et_content);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/*
	 * 激活新的Activity用于选择联系人
	 */
	public void selectContact(View v)
	{
		//开启一个Activity并获取其返回的结果
		startActivityForResult(new Intent(this, SelectContactActivity.class), 0);
		
	}
	public void sendSms(View v)
	{
		String number=model==null?et_phone.getText().toString().trim():model.getPhone();
		String sms=et_sms.getText().toString().trim();
		if(TextUtils.isEmpty(number))
		{
			Toast.makeText(this, "请输入号码或选择联系人", Toast.LENGTH_SHORT).show();
			return;
		}
		if(TextUtils.isEmpty(sms))
		{
			Toast.makeText(this, "请输入短信内容", Toast.LENGTH_SHORT).show();
			return;
		}
		SmsManager smsManager=SmsManager.getDefault();
		ArrayList<String> contents=smsManager.divideMessage(sms);
		
		try
		{
			smsManager.sendMultipartTextMessage(number, "", contents, null, null);
			Toast.makeText(this, "已发送", Toast.LENGTH_SHORT).show();
		} catch (Exception e)
		{
			Toast.makeText(this, "发送失败:"+e.getMessage(), Toast.LENGTH_SHORT).show();
		}
		
		
	}
	
	/*
	 * 新开启的Activity关闭时候调用
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		Bundle bundle=data.getExtras();
		model=(ContactModel) bundle.getSerializable("model");
		et_phone.setText(model.getName()+":"+model.getPhone());
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}

 

选择联系人AC:

package com.example.sms.sender.pro;

import java.util.List;

import com.contact.utils.ContactHelper;
import com.contact.utils.ContactModel;

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

public class SelectContactActivity extends Activity
{
	private ListView lv_contact;
	private List<ContactModel> contacts;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_contact);
		lv_contact=(ListView)findViewById(R.id.lv_contacts);
		contacts=ContactHelper.getContactList(this);
		lv_contact.setAdapter(new ContactAdapter());
		lv_contact.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				Intent intent=new Intent(SelectContactActivity.this,MainActivity.class);
				intent.putExtra("model", contacts.get(position));
				setResult(0, intent);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_contact, menu);
		return true;
	}

	private class ContactAdapter extends BaseAdapter{

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

		@Override
		public Object getItem(int position)
		{
			return contacts.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ContactModel model=contacts.get(position);
			View view=View.inflate(SelectContactActivity.this,R.layout.contact_item, null);
			TextView tv_name=(TextView) view.findViewById(R.id.tv_contact_name);
			TextView tv_phone=(TextView) view.findViewById(R.id.tv_contact_phone);
			tv_name.setText(model.getName());
			tv_phone.setText(model.getPhone());
			return view;
		}
		
	}
	
	
}

工具类:

package com.contact.utils;

import java.io.Serializable;

public class ContactModel implements Serializable

{

	private String contact_id;
	private String name;
	private String phone;
	private String email;

	/**
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * @return the phone
	 */
	public String getPhone()
	{
		if (null != phone)
		{
			return phone.replace("-", "").replace(" ", "");
		}
		return "";
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone)
	{
		this.phone = phone;
	}

	/**
	 * @return the email
	 */
	public String getEmail()
	{
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email)
	{
		this.email = email;
	}

	/**
	 * @return the contact_id
	 */
	public String getContact_id()
	{
		return contact_id;
	}

	/**
	 * @param contact_id
	 *            the contact_id to set
	 */
	public void setContact_id(String contact_id)
	{
		this.contact_id = contact_id;
	}

}

package com.contact.utils;

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

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

public class ContactHelper
{
	public static List<ContactModel> getContactList(Context context)
	{
		List<ContactModel> models = new ArrayList<ContactModel>();
		ContentResolver resolver = context.getContentResolver();

		Uri raw_contacts_uri = Uri.parse("content://com.android.contacts/raw_contacts");
		Uri data_uri = Uri.parse("content://com.android.contacts/data");

		Cursor cursor = resolver.query(raw_contacts_uri, null, null, null, null);
		while (cursor.moveToNext())
		{
			String contactid = cursor.getString(cursor.getColumnIndex("contact_id"));
			System.out.println(contactid + "\n");
			ContactModel model = new ContactModel();
			if (null != contactid)
			{
				Cursor data_cursor = resolver.query(data_uri, null, "raw_contact_id=?", new String[]
				{ contactid }, null);

				while (data_cursor.moveToNext())
				{
					String data1 = data_cursor.getString(data_cursor.getColumnIndex("data1"));
					String mimetype = data_cursor.getString(data_cursor.getColumnIndex("mimetype"));
					if (data1 != null && mimetype != null)
					{
						System.out.println("mimetype=" + mimetype + ";data1=" + data1);
						if ("vnd.android.cursor.item/name".equals(mimetype))
						{
							model.setName(data1);
						} else if ("vnd.android.cursor.item/phone_v2".equals(mimetype))
						{
							model.setPhone(data1);
						}
					}
				}
				if (model.getName() != null && model.getPhone() != null)
				{
					if (!TextUtils.isEmpty(model.getName()) && !TextUtils.isEmpty(model.getPhone()))
					{
						models.add(model);
					}
				}
				data_cursor.close();
			}
		}
		cursor.close();
		return models;
	}
}

 

你可能感兴趣的:(android,发送,短信)