监听器之短信发送

package com.example.eighteen;

import javax.security.auth.Destroyable;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/** * @author HD * @date 2015-12-7 * @package_name com.example.eighteen * @file_name MainActivity.java */
public class MainActivity extends Activity implements OnClickListener {
    private Button btnSendMsg;
    private EditText etPhonNumber;
    private EditText etMsgContent;
    private SendStatusReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter();
        filter.addAction("SEND_MESSAGE_ACTION");
        receiver = new SendStatusReceiver();
        registerReceiver(receiver, filter);

        btnSendMsg = (Button) findViewById(R.id.btnSendMsg);
        etPhonNumber = (EditText) findViewById(R.id.etPhoneNumber);
        etMsgContent = (EditText) findViewById(R.id.etMsgContent);

        btnSendMsg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO 自动生成的方法存根
        SmsManager smsManager = SmsManager.getDefault();
        Intent intent = new Intent();
        intent.setAction("SEND_MESSAGE_ACTION");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0, intent, 0);
        smsManager.sendTextMessage(etPhonNumber.getText().toString(), null,
                etMsgContent.getText().toString(), pendingIntent, null);
    }

    class SendStatusReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO 自动生成的方法存根
            if (getResultCode() == Activity.RESULT_OK) {
                Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(MainActivity.this, "发送失败", Toast.LENGTH_SHORT)
                        .show();
            }
        }

    }

    @Override
    protected void onDestroy() {
        // TODO 自动生成的方法存根
        super.onDestroy();
        unregisterReceiver(receiver);
    }

}
<LinearLayout 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:orientation="vertical" 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.eighteen.MainActivity" >

    <EditText  android:id="@+id/etPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="电话号码" />

    <EditText  android:id="@+id/etMsgContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="短信内容" />

    <Button  android:id="@+id/btnSendMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="发送短信" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.eighteen" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

你可能感兴趣的:(监听器之短信发送)