打电话,发送短信

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值