无法从SmsReceiver调用不同类的方法

时间:2013-02-27 16:14:41

标签: android sms broadcastreceiver

我正在开发一个关于android的sms应用程序发送者类型和点击发送消息消息在类Ecc中被操作并被发送到接收者。接收方必须阅读此消息并将其发送到其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> 

我没有收到任何错误。发送方的功能正如我希望接收方获取消息但没有任何反应。

2 个答案:

答案 0 :(得分:1)

,如Ecc类:

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

您已使用Ecc活动类型参数创建了SMSTest构造函数,但是您在SMSReceiver内创建对象时正在传递BroadcastReceiver

<强>解决方案:

而不是通过创建Activity实例调用Activity的方法,为Activity或BroadcastReceiver之间的共享方法创建一个单独的普通java类。而不是传递任何组件实例传递上下文发送短信作为:

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

现在使用context发送短信并在Ecc内创建BroadcastReceiver类实例:

Ecc ecc;
ecc=new Ecc(context);  //<< pass context instead of this

答案 1 :(得分:0)

请在Manifest文件中注册您的广播

<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);