关于意图Intent:

 

       显式意图:必须指定要激活的组件的完整包名和类名(应用程序之间耦合在一起)
一般激活自己应用的组件的时候采用显式意图

隐式意图:只需要指定动作和数据就可以(好处是应用程序之间没有耦合)
激活别人写的应用,不需要关心对方的包名和类名

此处使用的是显示意图

清单文件中中需要添加以下内容:

        <activity android:name="com.zhangbz.smshelper.ListSmsActivity"></activity>
        <activity android:name="com.zhangbz.smshelper.ListNumberActivity"></activity>

 

package com.zhangbz.smshelper;

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

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);
	}
	 /**
	  * 选择短信按钮方法
	  * @param v
	  */
	public void selectSms(View v) {
		Intent intent = new Intent(this, ListSmsActivity.class);
		//开启一个新的界面,并且获取界面的返回值
		//startActivity(intent);
		startActivityForResult(intent, 0);
	}
	/**
	 * 选择电话号码按钮fangfa
	 * @param v
	 */
	public void selectNumber(View v) {
		Intent intent = new Intent(this, ListNumberActivity.class);
		startActivityForResult(intent, 1);
		
	}
	
	/**
	 * 接收返回的结果
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(data != null) {
			String smsinfo = data.getStringExtra("smsinfo");
			if(requestCode == 0){  //标示不同的数据来源
				et_content.setText(smsinfo);
			} else if(requestCode == 1){
				et_number.setText(smsinfo);
			}
		}
	}
	
	/**
	 * 发送短信方法
	 * @param v
	 */
	public void sendSms(View v) {
		String content = et_content.getText().toString().trim();
		String number = et_number.getText().toString().trim();
		SmsManager.getDefault().sendTextMessage(number, null, content, null, null);
		Toast.makeText(this, "发送成功", 0).show();
		
	}
}


以ListSmsActivity.java为例,ListNumberActivity.java同。

package com.zhangbz.smshelper;

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 ListSmsActivity extends Activity {

	private ListView lv;
	private String[] objects = {"sdfsfafafasf","sdffdfafafafasfsa" ,"sdfhjkfhlkjfhfjflfhjla", "sdfja;lkfjfkajf;afjsd;fjasdf;sadfjklsad"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sms);
		//ListView
		lv = (ListView) findViewById(R.id.lv);
		// 适配器
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.sms_item, R.id.tv_info, objects));
		//设置点击监听方法
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				String smsinfo = objects[position];
				Intent data = new Intent();
				data.putExtra("smsinfo", smsinfo);
				//设置数据
				setResult(0, data);
				//关闭掉当前的activity,并且回传数据onActivityResult()
				finish();
			}
		});
	}
}


源码链接

 

 posted on 2014-12-04 16:30  zhangbz  阅读(369)  评论(0编辑  收藏  举报