如何在广播器onReceive中更改静态变量

时间:2013-12-31 06:59:37

标签: android broadcastreceiver

我想获取每个电话号码的发送状态,因此我定义了一个变量statusMap来记录状态,0表示成功,1表示失败。我在statusMap函数处为onReceive赋值,但之后statusMap的值仍为空。如何在onReceive函数

中更改静态值
package com.mem.memsms;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import android.os.Bundle;
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.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SendMessage extends Activity {
private EditText editText;
private Button button;
private Intent intent;
private SendBroadcast mSendReceiver;

private HashMap<String, String> hashMap;
private HashMap<String, String> statusMap = new HashMap<String, String>();
private Queue<String> numbers;
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_message);
    editText = (EditText) this.findViewById(R.id.message);
    intent = getIntent();
    hashMap = (HashMap<String, String>) intent.getSerializableExtra("data");
    numbers = new LinkedList<String>();

    button = (Button) this.findViewById(R.id.sendmessage);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            numbers.clear();
            editText.setEnabled(false);
            final String text = editText.getText().toString();
            if (text.trim() == "") {
                editText.setHint(R.string.msg_null);
                return;
            }
            Iterator<Entry<String, String>> iter = hashMap.entrySet()
                    .iterator();
            while (iter.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
                        .next();
                String number = entry.getKey();
                String content = entry.getValue();
                numbers.offer(number);
                Sendmsg(number, content + text);
            }
            intent.putExtra("data2", statusMap);
            setResult(RESULT_OK, intent);
            //Log.i("msg", "statusMap length is" + statusMap.size());
            // back to contacts
            SendMessage.this.finish();
        }

    });

}

private class SendBroadcast extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String n = numbers.poll();
        switch (getResultCode()) {
        case RESULT_OK:
            Log.i("msg", "c:ok" + n);
            statusMap.put(n, "0");
            break;

        default:
            Log.i("msg", "c:failed" + n);
            statusMap.put(n, "1");
            break;
        }
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    this.unregisterReceiver(mSendReceiver);
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub

    mSendReceiver = new SendBroadcast();
    IntentFilter mSendFilter = new IntentFilter(SENT_SMS_ACTION);
    this.registerReceiver(mSendReceiver, mSendFilter);
    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.send_message, menu);
    return true;
}

private void Sendmsg(String number, String content) {

    Intent sentIntent = new Intent(SENT_SMS_ACTION);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
            0);
    //sentIntent.putExtra("status", statusMap);
    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(number, null, content, sentPI, null);

}
}

1 个答案:

答案 0 :(得分:1)

删除它:

intent.putExtra("data2", statusMap);
        setResult(RESULT_OK, intent);
        //Log.i("msg", "statusMap length is" + statusMap.size());
        // back to contacts
        SendMessage.this.finish();
来自onClick的

代码并将其放入onReceive:

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String n = numbers.poll();
    switch (getResultCode()) {
    case RESULT_OK:
        Log.i("msg", "c:ok" + n);
        statusMap.put(n, "0");
        break;

    default:
        Log.i("msg", "c:failed" + n);
        statusMap.put(n, "1");
        break;
    }
    if(hashMap.size()==statusMap.size()){
        intent.putExtra("data2", statusMap);
        SendMessage.this.setResult(RESULT_OK, intent);
        //Log.i("msg", "statusMap length is" + statusMap.size());
        // back to contacts
        SendMessage.this.finish();
   }
}

问题是onClick事件首先完成,你调用set result并完成此活动,然后onReceive被调用,所以当你的地图更新时结果已经发送