我有这个简单的短信监听器类:
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 那是空的。
我正在尝试制作一个短信监听器,当收到短信时,它会更新我正在运行的应用程序的短信列表。似乎我无法从广播侦听器调用活动方法。谁能解释一下我该如何解决这个问题?