1,打电话
在manifest文件中加入拨打权限
然后设置Intent 的Action 与 Data即可完成简单的拨打电话
这里要用到一个短信管理类SmsManager
在manifest文件中加入拨打权限
<uses-permission android:name="android.permission.CALL_PHONE" />
然后设置Intent 的Action 与 Data即可完成简单的拨打电话
package com.lc.call;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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 {
private Button callmemaybe=null;
private EditText call=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call=(EditText)findViewById(R.id.call);
callmemaybe=(Button)findViewById(R.id.callmemaybe);
callmemaybe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String number=call.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
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
2,发短信.
同理,加入发短信权限
<uses-permission android:name="android.permission.SEND_SMS"/>"
这里要用到一个短信管理类SmsManager
package com.lc.sms;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText smsnum;
private EditText smscontent;
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsnum=(EditText)findViewById(R.id.telephonenum);
smscontent=(EditText)findViewById(R.id.smscontent);
myButton=(Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String num=smsnum.getText().toString();
String content=smscontent.getText().toString();
SmsManager sms=SmsManager.getDefault();
//对内容进行分割放入数组
ArrayList<String>texts=sms.divideMessage(content);
for(String text : texts )
{
//params
//1.发送到的地址,2.短信中心的地址,3.内容,4.发送状态,5.对方是否收到短信的状态
sms.sendTextMessage(num, null, text, null, null);
}
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG).show();
//smscontent.setText("");
}
});
}
}