0

我有这个简单的短信监听器类:

package net.albanx.smspack;

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 ReceiverListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";        
        ReceiverActivity addtolist = new ReceiverActivity();
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            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]);     
                addtolist.addSMS(msgs[i].getOriginatingAddress(), "now", msgs[i].getMessageBody().toString());

                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }

            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                       
    }
}

方法时出现空点执行错误

addtolist.addSMS(msgs[i].getOriginatingAddress(), "now", msgs[i].getMessageBody().toString());

叫做。此方法是活动的一部分:

public void addSMS(String sms_adress, String sms_date, String message)
{
    LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    // from Number label
    TextView from=new TextView(this);
    from.setLayoutParams(lparams);
    from.setBackgroundColor(Color.argb(200, 0, 0, 0));
    from.setTextAppearance(getApplicationContext(),  R.style.from_style);
    from.setText(this.getString(R.string.label_from_sms)+":"+ sms_adress);
    linearLayout.addView(from);
}

问题出在这一行:

TextView from=new TextView(this);

参考 this 那是空的。

我正在尝试制作一个短信监听器,当收到短信时,它会更新我正在运行的应用程序的短信列表。似乎我无法从广播侦听器调用活动方法。谁能解释一下我该如何解决这个问题?

4

2 回答 2

2

我将再一次回答这类问题(并希望我每次都有一美元)......

ReceiverActivity addtolist = new ReceiverActivity();

您不能实例化Activityusing new

Activity是 Android 中的一个特例,任何扩展类的新实例Activity只能由 Android 操作系统本身创建。您永远不应尝试从其自身Activity外部访问任何数据成员或方法。Activity

如果您需要将数据传递给一个Activity,然后使用它启动它startActivity(...)并通过Intent额外的传递数据。或者,将 your 嵌套BroadcastReceiverActivity.

于 2012-06-29T23:24:37.997 回答
0

您不能使用 new 关键字实例化 Activity。因此,如果要将值从广播接收器传递给 Activity,则使用 Intent 将值传递为:

将 getOriginatingAddress 和 getMessageBody 传递给您的活动:

public class ReceiverListener extends BroadcastReceiver
{
List<String> getOriginatingAddresslist = new ArrayList<String>();
List<String> getMessageBodylist = new ArrayList<String>();
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";        
        ReceiverActivity addtolist = new ReceiverActivity();
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            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]);
                getOriginatingAddresslist.add(msgs[i].getOriginatingAddress());
                getMessageBodylist.add(msgs[i].getMessageBody().toString());
            }
         Intent msgIntent = new Intent(context,YourActivity.class); 
         msgIntent.putStringArrayListExtra("getOriginatingAddresslist", getOriginatingAddresslist); 
         msgIntent.putStringArrayListExtra("getMessageBodylist", getMessageBodylist);  
         startActivity(msgIntent); 

并在您的活动中接收为:

public class YourActivity extends MapActivity 
{
List<String> getOriginatingAddresslist = new ArrayList<String>();
List<String> getMessageBodylist = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent = this.getIntent();
        getOriginatingAddresslist = intent.getStringArrayListExtra("getOriginatingAddresslist");  
        getMessageBodylist = intent.getStringArrayListExtra("getMessageBodylist");
        for (int i=0; i<getMessageBodylist.size(); i++){

                addSMS(getOriginatingAddresslist.get(i).toString(), "now", getMessageBodylist.get(i).toString());

            }

如果 Activity 已经在运行,则用于处理新意图,然后使用 o nNewIntent (意图意图)

或者

您还可以使用自定义广播接收器从 ReceiverListener 接收活动中的值,请参阅

使用接收器自定义意图和广播

于 2012-06-29T23:12:18.647 回答