现在的位置: 首页 > 综合 > 正文

短信发送状态监听

2018年03月31日 ⁄ 综合 ⁄ 共 6455字 ⁄ 字号 评论关闭

 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:gravity="center_horizontal"

  android:background="#3399ff">

  <TableLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content"

     android:layout_marginLeft="8dp">

     <TableRow>

       <TextView

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="收信人:"

         android:textColor="#ffffff"/>

       <EditText

         android:id="@+id/tel"

         android:layout_marginRight="8dp"

         android:numeric="integer"

         android:layout_width="240dp"

         android:layout_height="wrap_content"/>

     </TableRow>

     <View

       android:layout_height="2px"

       android:background="#FF909090" />

     <TableRow>

       <TextView

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="内  容:"

         android:textColor="#ffffff"/>

       <EditText

         android:id="@+id/content"

         android:lines="6"

         android:gravity="top"

         android:layout_width="10dp"

         android:layout_height="wrap_content" />

     </TableRow>

     <View

       android:layout_height="2px"

       android:background="#FF909090" />

  </TableLayout>

  <Button

     android:id="@+id/send"

     android:layout_width="100dp"

     android:layout_height="wrap_content"

     android:text="发送短信" />

</LinearLayout>

 

 

 

 

 

 

在SMSDeliveredBroadcastReceiver.java中:

 

package com.li.smslistener;

 

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.SmsManager;

import android.widget.Toast;

 

public class SMSDeliveredBroadcastReceiver extends BroadcastReceiver {

  @Override

  public void onReceive(Context context, Intent intent) {

     if (intent.getAction().equals("SMS_DELIVERED_ACTION")) { // 短信发送

       switch (super.getResultCode()) { // 操作结果

       case Activity.RESULT_OK:

         Toast.makeText(context, "短信已接收!", Toast.LENGTH_SHORT).show();

         break;

       case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

         Toast.makeText(context, "短信发送失败!", Toast.LENGTH_SHORT).show();

         break;

       }

     }

  }

 

}

 

 

 

 

 

 

在SMSSendBroadcastReceiver.java中:

 

package com.li.smslistener;

 

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.SmsManager;

import android.widget.Toast;

 

public class SMSSendBroadcastReceiver extends BroadcastReceiver {

 

  @Override

  public void onReceive(Context context, Intent intent) {

     if (intent.getAction().equals("SMS_SEND_ACTION")) { // 短信发送

       switch (super.getResultCode()) { // 操作结果

       case Activity.RESULT_OK:

         Toast.makeText(context, "短信已发送!", Toast.LENGTH_SHORT).show();

         break;

       case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

         Toast.makeText(context, "短信发送失败!", Toast.LENGTH_SHORT).show();

         break;

       }

     }

  }

 

}

 

 

 

 

 

 

 

在MySMSListenerDemo.java中:

 

package com.li.smslistener;

 

import java.util.Iterator;

import java.util.List;

 

import android.app.Activity;

import android.app.PendingIntent;

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;

 

public class MySMSListenerDemo extends Activity {

  private EditText tel = null;

  private EditText content = null;

  private Button send = null;

  private SMSSendBroadcastReceiver sendRec = null;

  private SMSDeliveredBroadcastReceiver delRec = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.sendRec = new SMSSendBroadcastReceiver();

     this.delRec = new SMSDeliveredBroadcastReceiver();

     this.tel = (EditText) super.findViewById(R.id.tel);

     this.content = (EditText) super.findViewById(R.id.content);

     this.send = (Button) super.findViewById(R.id.send);

     this.send.setOnClickListener(new SendOnClickListenerImpl());

  }

 

  private class SendOnClickListenerImpl implements OnClickListener {

     public void onClick(View v) {

       Intent sentIntent = new Intent("SMS_SEND_ACTION");

       Intent deliveredIntent = new Intent("SMS_DELIVERED_ACTION");

       SmsManager smsManager = SmsManager.getDefault();

       String telphone = MySMSListenerDemo.this.tel.getText().toString();

       String content = MySMSListenerDemo.this.content.getText()

            .toString();

       PendingIntent sendPendIntent = PendingIntent.getBroadcast(

            MySMSListenerDemo.this, 0, sentIntent, 0);

       PendingIntent deliveredPendIntent = PendingIntent.getBroadcast(

            MySMSListenerDemo.this, 0, deliveredIntent, 0);

       MySMSListenerDemo.this.registerReceiver(

            MySMSListenerDemo.this.sendRec, new IntentFilter(

                "SMS_SEND_ACTION"));

       MySMSListenerDemo.this.registerReceiver(

            MySMSListenerDemo.this.delRec, new IntentFilter(

                "SMS_DELIVERED_ACTION"));

       if (content.length() > 70) {

         List<String> msgs = smsManager.divideMessage(content); // 拆分信息

         Iterator<String> iter = msgs.iterator();

         while (iter.hasNext()) {

            String msg = iter.next();

            smsManager.sendTextMessage(telphone, null, msg,

                sendPendIntent, deliveredPendIntent);

         }

       } else {

         smsManager.sendTextMessage(telphone, null, content,

              sendPendIntent, deliveredPendIntent);

       }

     }

 

  }

}

 

 

 

 

 

 

 

修改AndroidManifest.xml:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.smslistener"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SEND_SMS" />

  <uses-permission android:name="android.permission.RECEIVE_SMS" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MySMSListenerDemo"

            android:label="@string/title_activity_my_smslistener_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 

抱歉!评论已关闭.