您的位置:首页 > 其它

打电话,发送短信

2015-09-28 10:37 726 查看
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button phone = (Button) findViewById(R.id.phone);
Button massage = (Button) findViewById(R.id.massage);
final EditText mobileText = (EditText) findViewById(R.id.mobile);
final EditText msgText = (EditText) findViewById(R.id.etmsg);

phone.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String mobile = mobileText.getText().toString();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:"+ mobile));
startActivity(intent);
}
});

massage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String phone=mobileText.getText().toString().trim();
String msg=msgText.getText().toString().trim();
send(phone,msg);
}
});

}

private void send(String number, String message){
String SENT = "sms_sent";
String DELIVERED = "sms_delivered";

PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(DELIVERED), 0);

registerReceiver(new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
switch(getResultCode())
{
case Activity.RESULT_OK:
Log.i("====>", "Activity.RESULT_OK");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.i("====>", "RESULT_ERROR_NO_SERVICE");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.i("====>", "RESULT_ERROR_NULL_PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.i("====>", "RESULT_ERROR_RADIO_OFF");
break;
}
}
}, new IntentFilter(SENT));

registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
switch(getResultCode())
{
case Activity.RESULT_OK:
Log.i("====>", "RESULT_OK");
break;
case Activity.RESULT_CANCELED:
Log.i("=====>", "RESULT_CANCELED");
break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager smsm = SmsManager.getDefault();
smsm.sendTextMessage(number, null, message, sentPI, deliveredPI);
}

}

layout中的界面布局代码:

<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="com.example.test.MainActivity$PlaceholderFragment" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入电话号码" />

<EditText
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="请输入号码"
android:inputType="phone" >

<requestFocus />
</EditText>
<EditText
android:id="@+id/etmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mobile"
android:hint="请输入消息"
android:ems="10" />

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etmsg"
android:layout_below="@+id/etmsg" >

<Button
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="拨出此号码" />

<Button
android:id="@+id/massage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/phone"
android:text="向此号发短信" />

</RelativeLayout>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: