如何从broadcastReciver类发送一个数组MainActivity [英] How to send an array from broadcastReciver class to MainActivity

查看:173
本文介绍了如何从broadcastReciver类发送一个数组MainActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者到Android和我在应用程序中查找有抛出短信特效药。所有药店的工作。因此,应用程序收到一个短信,其中包含有药品的所有药店的名称,它们保存在一个名为OurString一个数组,但此数组中。我已经TRID发送一个字符串广播接收器类和它的工作,但是当我尝试了数组didnt worked.so如何发送数组到其他类,查看列表视图中药店的名称。
       这是我的BroadcastRecieve类:

i am beginner to android and i am working in application that looks for pharmacies that have a specific medicine .all that throw SMS . so the app receive a SMS that contains the name of all pharmacies that have the medicine,saved them in an array named OurString but this array is in broadcast receiver class .i have trid to send a string and it worked but when i tried the array didnt worked.so how do i send array to other class to view the pharmacies's names in a listView.
This is my BroadcastRecieve class:

 public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null ;
    String[] OurString = null;
    String str = " ";
    if(bundle != null)
    {
        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]);
            str += msgs[i].getMessageBody().toString();
            OurString = msgs[i].getMessageBody().toString().split(" ");
            str += " : ";

        }
        for(int i=0;i<OurString.length;i++)
        {
            str += "(";
            str += OurString[i];
            str += ")";

        }
        Toast.makeText(context,str,Toast.LENGTH_SHORT).show();
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("SMS_RECEIVED_ACTION");
        broadcastIntent.putExtra("sms", str);
        context.sendBroadcast(broadcastIntent);




    }
}

这就是我要发送的字符串,我用药店阵列测试列表视图,我想它与OurString替换:

This is where i want to send String,i used pharmacies array to test the listview and i want to replace it with the OurString:

public class MedName extends ListActivity {
String[] pharmacies = {"pharmacy 1","pharmacy 2"};
Button btnSendSMS,btnShowMap;
EditText editText;
IntentFilter intentFilter,intentFilter1;

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        TextView SMSes = (TextView)findViewById(R.id.tv);
        SMSes.setText(intent.getExtras().getString("sms"));

    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medicinename);
    btnShowMap = (Button) findViewById(R.id.btnShowMap);
    btnShowMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent showmap = new Intent("com.example.rana.me_lo.MapsActivity");
            startActivity(showmap);
        }
    });
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    editText = (EditText) findViewById(R.id.etMeName);
    btnSendSMS.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           sendSMS("5666",editText.getText().toString());
      }
    });
    TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
    tabHost.setup();
    TabHost.TabSpec spec1 = tabHost.newTabSpec("Search");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator(" Search",getResources().getDrawable(R.drawable.picture));

    TabHost.TabSpec spec2 = tabHost.newTabSpec("Result");
    spec2.setContent(R.id.tab2);
    spec2.setIndicator("Result");
     tabHost.addTab(spec1);
     tabHost.addTab(spec2);
     intentFilter =new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED_ACTION");

     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pharmacies));
}

private void sendSMS(String number, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI = PendingIntent.getBroadcast(this,0,new Intent(SENT),0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this,0,new Intent(DELIVERED),0);
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(),"SMS sent",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                   Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    },new IntentFilter(SENT));
    registerReceiver(new BroadcastReceiver() {
      @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    },new IntentFilter(DELIVERED));
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number,null,message,sentPI,deliveredPI);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

   Toast.makeText(this,"You have selected "+pharmacies[position],Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
    registerReceiver(intentReceiver,intentFilter);

    super.onResume();
}

@Override
protected void onPause() {
    unregisterReceiver(intentReceiver);

    super.onPause();
}

}

推荐答案

我曾面临同样的问题,设置标志,如下为我工作
       意向意图=新的意图(背景下,MainActivity.class);     intent.putStringArrayListExtra(名单,arraylistOfStrings); PendingIntent pIntent = PendingIntent.getActivity(此,0,                 意图,PendingIntent.FLAG_CANCEL_CURRENT); 在MainActivity

I had faced same problem and setting flag as below worked for me
Intent intent = new Intent(context,MainActivity.class); intent.putStringArrayListExtra("list", arraylistOfStrings); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); In MainActivity

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
arrayList= bundle.getStringArrayList("list");
}

这篇关于如何从broadcastReciver类发送一个数组MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆