如何在广播接收器中使用全局字符串?

How to use a global string in Broadcast receiver?

如何在广播接收器中使用全局字符串? sms 变量有什么问题?

public class MainActivity extends Activity {
    String sms=""; //here is wrong???
    TextView view = new TextView(this);
    Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null ,null);
    while (cur.moveToNext()) {
        if(cur.getString(13).startsWith("\n ")){
            sms +=cur.getString(13)+"\n";
        }             
    }

    TextView text = (TextView) findViewById(R.id.tabel);
    text.setText(sms);
    //here works well

    private final BroadcastReceiver SMSReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              Bundle bundle=intent.getExtras();
              Object messages[]=(Object[])bundle.get("pdus");
              SmsMessage smsMessage[]=new SmsMessage[messages.length];
              for (int i=0; i < messages.length; i++) {
                     smsMessage[i]=SmsMessage.createFromPdu((byte[])messages[i]);
                 }

              if(smsMessage[0].getMessageBody().startsWith("\n ")){
                     sms +=smsMessage[0].getMessageBody(); 
                     //or here is wrong?
                }

          TextView text = (TextView) findViewById(R.id.tabel);
          text.setText(sms);

我想制作一个包含所有收到的短信的列表,当我收到短信时必须更新此列表。

我所做的是创建一个 class 并将所有静态变量放入其中。

例如,

class Values {
    static public Activity frontActivity = null;
    static public int currentStatus = Status.IDLE;
}

并像

一样访问它们
Values.frontActivity = this;
Values.currentStatis = Status.ACTIVE;