Android入门:短信和拨打电话

时间:2022-11-10 10:19:09

Android入门:短信和拨打电话Android入门:短信和拨打电话

Android入门:短信和拨打电话

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入门:短信和拨打电话

Android入门:短信和拨打电话

Android入门:短信和拨打电话