0

在我的 android 项目中,我需要在 SMS 文本进入时检测和读取它们,然后从另一个类调用一个函数。

如何将以下代码放在一个类中并在清单中正确定义它。如果我把它放在另一个 Java 文件上,那么我不知道如何从另一个 Java 文件调用函数。我尝试将此代码放在我的主类中,但在我的应用程序中,它就崩溃了。

public class SMSReceiverActivity extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Parse the SMS.
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null)
        {
            // Retrieve the SMS.
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                // In case of a particular App / Service.
                //if(msgs[i].getOriginatingAddress().equals("+91XXX"))
                //{
                //str += "SMS from " + msgs[i].getOriginatingAddress();
                //str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
                //}
            }
            if (str != "") { // remove the last \n
                str = str.substring(0, str.length()-1);
            }

            Reusable_CodeActivity.alert(my_ViewActivity.this, "AAAAAAAAAAA");
            try {
                //my_ViewActivity.this.handle_incoming_message(str);
            } catch(Exception e) {

            }
        }
    }
}
4

2 回答 2

0

您接收短信的代码是正确的。但是当您BroadCastReceiver在 Activity 类中使​​用时,您需要在 onStop() 方法中按如下方式取消注册它。我认为这就是您的活动令人沮丧的原因。

@Override
protected void onStop()
{
    unregisterReceiver(mReceiver);
    super.onStop();
}
于 2012-08-28T02:28:06.560 回答
0

如果我正确理解您的问题,您希望在显示活动时接收广播(当短信到达时

如果是这样,我不久前找到了一个很好的模式来做到这一点(不记得我从哪里得到它,很高兴感谢原作者)。

首先,创建一个接口:

public interface Receiver {
    public void onReceive(Context context, Intent intent);
}

现在创建一个 BroadcastReceiver 的子类,我们可以在运行时动态注册它:

public class PortableReceiver extends BroadcastReceiver {
    private Receiver mReceiver;

    public void setReceiver(Receiver receiver) {
        mReceiver = receiver;
    }

    public Receiver getReceiver() {
        return mReceiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (mReceiver != null)
            mReceiver.onReceive(context, intent);
        else
            Log.w("PortableReceiver", "Dropping received Result");
    }
}

现在我们可以在活动类中使用 PortableReceiver:

public class MyActivity implements Receiver {
    private PortableReceiver mReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mReceiver = new PortableReceiver();
        mReceiver.setReceiver(this);
    }

    @Override
    public void onResume() {
        super.onResume();

        // Register the receiver
        IntentFilter filter = new IntentFilter();
        filter.addAction("_my_intent_action"); //TODO: add actions to filter just as you would in your AndroidManifest
        registerReceiver(mReceiver, filter, null, null);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO: Your code goes here!
    }
}

现在您的 Activity 类直接处理广播,因此您可以访问它所有可用的功能。

请注意,如果您想从 BroadcastReceiver开始一个活动,那就有点不同了。如果是这样,我建议您查看http://www.vogella.com/articles/AndroidServices/article.html#pre_broadcastreceiver

于 2012-08-28T05:02:02.403 回答