0

我正在开发 android 上的短信应用程序,发送方类型并单击发送消息,消息在 Ecc 类中进行操作并发送到接收方。接收者必须阅读此消息并将其发送到其 Ecc 类,在该类中对其进行操作并发送回发送者。我能够将消息发送给接收者,但接收者端没有任何反应。

这是 SmsReceiver 类

 package com.example.smsTest;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.telephony.SmsMessage;
 import android.widget.Toast;

 public class SMSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle=intent.getExtras();
    Object[] messages=(Object[])bundle.get("pdus");
    SmsMessage[] sms=new SmsMessage[messages.length];

    Ecc ecc;
    ecc = new Ecc(context);
    String msg_receive = intent.getStringExtra("message1"); 
    if (msg_receive != null)  { 

    for(int n=0;n<messages.length;n++)
        {
            sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);

        }

    for(SmsMessage msg:sms)
        {
            String ph=msg.getDisplayOriginatingAddress();
            String body=msg.getMessageBody();
            ecc.recv(ph,body);          
            Toast.makeText(context,"ecc entered at receiver", Toast.LENGTH_SHORT).show();

            SMSTest.update("\n From:" +ph+"\n"+"message:"+body+"\n");

            Toast.makeText(context,"updated", 
                    Toast.LENGTH_SHORT).show();

        }

    }
  } 
}       

这是ecc类

package com.example.smsTest;
import java.util.*;
public class Ecc{

//code
private SMSTest smsTest;


// Constructor
public Ecc(SMSTest smsTest) {
    this.smsTest = smsTest;
}


public void recv(String phn, String smsg){

    String[] spl1=str.split("\\.");
 if("abc".equals(spl1[0])){
    //code
   sendSMS(ph,smsg)  
 }  
 if("pmg".equals(spl1[0])){
   //code 
sendSMS(ph,smsg)  
 }  
}

//code
//---sends a SMS message to another device---
    @TargetApi(Build.VERSION_CODES.DONUT)
    @SuppressLint("NewApi")    
    public void sendSMS(String phoneNumber, String message)
    {      
        /*
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, test.class), 0);                
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);        
        */

        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(smsTest, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(smsTest, 0,
            new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(smsTest, "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(smsTest, "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(smsTest, "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(smsTest, "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(smsTest, "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @TargetApi(Build.VERSION_CODES.DONUT)
            @SuppressLint("NewApi")
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(smsTest, "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(smsTest, "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                                break;                  
                }
            }
        },new IntentFilter(DELIVERED));        

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);               
    }

    private void registerReceiver(BroadcastReceiver broadcastReceiver,
            IntentFilter intentFilter) {
        // TODO Auto-generated method stub

    }


 }

这里是 SMSTest 类

package com.example.smsTest;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


@TargetApi(Build.VERSION_CODES.DONUT)
@SuppressLint("NewApi")
public class SMSTest extends Activity 
{
public final static String SMS_Message = "com.example.SMSTest.MESSAGE";
public final static String SMS_Phone = "com.example.SMSTest.MESSAGE";
static TextView txtmsg;
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
Ecc ecc; 

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txtmsg=(TextView)findViewById(R.id.txtmsg);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);

    ecc=new Ecc(this);

    /*
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
    sendIntent.setType("vnd.android-dir/mms-sms");
    startActivity(sendIntent);
    */

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {               
            String phoneNo = txtPhoneNo.getText().toString(); 
            String message = txtMessage.getText().toString();
            message="pmg."+message;
            if (phoneNo.length()>0 && message.length()>0){
                System.out.println("details verified");    
                Toast.makeText(getBaseContext(),"Ecc call clicked", 
                    Toast.LENGTH_SHORT).show();
                ecc.recv(phoneNo, message);
            }
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();

        }
    });


}

public static void update(String msg) {
    // TODO Auto-generated method stub
    txtmsg.append(msg);

}

}

这是清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.smsTest"
  android:versionCode="1"
  android:versionName="1.0">
      <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-feature android:name="android.hardware.telephony" /> 
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />


  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SMSTest"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".SMSReceiver"> 
        <intent-filter >
               <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
  </application>
</manifest> 

我没有收到任何错误。发送方功能如我所愿,接收方收到消息,但没有任何反应。

4

2 回答 2

1

Ecc课堂上:

// Constructor
public Ecc(SMSTest smsTest) {   //<<<<
    this.smsTest = smsTest;
}

您已经Ecc使用 Activity 类型参数创建了构造函数,SMSTest但是您在内部创建对象时传递SMSReceiverBroadcastReceiver

解决方案:

而不是通过创建 Activity 实例来调用 Activity 的方法,而是创建一个单独的普通 java 类,用于在 Activity 或BroadcastReceiver. 而不是传递任何组件实例传递上下文以发送短信:

Context context ;
    // Constructor
    public Ecc(Context context) {   //<<<<
        this.context = context;
    }

现在context用于发送 SMS 并 Ecc在内部创建类实例BroadcastReceiver

Ecc ecc;
ecc=new Ecc(context);  //<< pass context instead of this
于 2013-02-27T16:33:36.713 回答
0

请像这样在 Manifast 文件中注册您的广播

<receiver android:name="your package name and class name">  </receiver>

或尝试在代码中注册您的广播,而不是像这样的清单

IntentFilter in_filter = new IntentFilter("android.intent.action.PHONE_STATE");
     Incoming = new BroadcastReceiver()
            {   
            @Override
            public void onReceive(final Context context, Intent intent) {
                // TODO Auto-generated method stub
                  {
                      telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                      callEventListener = new PhoneStateListener(){
                          public void onCallStateChanged(int state, String incomingNumber) {
                                super.onCallStateChanged(state, incomingNumber);
                                if (state == TelephonyManager.CALL_STATE_IDLE){
                                     System.out.println("Idel State phone");
                                }
                                else if (state == TelephonyManager.CALL_STATE_RINGING){
                                    context.stopService(new Intent(context, myPlayService.class));
                                    System.out.println("phone ringing");
                                }
                            }
                        };
                telephonyManager.listen(callEventListener, PhoneStateListener.LISTEN_CALL_STATE);
                      }
                 }
            };

            this.registerReceiver(Incoming, in_filter);         
于 2013-02-27T16:24:43.070 回答