码迷,mamicode.com
首页 > 移动开发 > 详细

Android入门:短信和拨打电话

时间:2015-07-22 23:01:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:安卓

技术分享技术分享

技术分享

package com.example.sendsms;

import java.util.ArrayList;

import android.R.array;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final EditText txtNum = (EditText) this.findViewById(R.id.txtNumber);
		final EditText txtBody = (EditText) this.findViewById(R.id.txtBody);
		
		Button btnSms = (Button) this.findViewById(R.id.btnSend);
		btnSms.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String num = txtNum.getText().toString();
				String body = txtBody.getText().toString();
				
				SmsManager manager = SmsManager.getDefault();
				ArrayList<String> list = manager.divideMessage(body);
				for(String text:list){
					manager.sendTextMessage(num, null, body, null, null);
				}
			}
		});
		
		Button btnCall = (Button) this.findViewById(R.id.btnCall);
		btnCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String num = txtNum.getText().toString();
				
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+num));//tel:是必须要这样
				startActivity(intent);
			}
		});
	}

	@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;
	}
}

(1)SmsManager manager = SmsManager.getDefault();   //获得默认的消息管理器

(2)ArrayList<String> list = manager.divideMessage(String txt);  //拆分长短信

(3)manager.sendTextMessage(String phone,null,String content,null,null);  //发送短信


加入到权限

技术分享

技术分享

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android入门:短信和拨打电话

标签:安卓

原文地址:http://blog.csdn.net/xinwen1995/article/details/47009605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!