如何将数组从BroadcastReciver类发送到MainActivity

问题描述:

我是Android的初学者,我正在寻找具有特定药物的药房的应用程序,所有这些都会抛出短信。所以应用程序会收到一个包含所有药物的名称的短信,将它们保存在一个名为OurString的数组中,但是这个数组在广播接收器类中.i有trid发送一个字符串并且它能够工作,但是当我尝试数组时没有working.so我怎么发送数组到其他类来查看listView中的药店的名称。
这是我BroadcastRecieve类:如何将数组从BroadcastReciver类发送到MainActivity

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来取代它:

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(); 
} 

}

+0

的广播接收器可以在后台运行,并且在MainActivity将当时不会打开。在你的场景中,当用户在应用程序上时,广播接收器运行... – Psypher

+0

我没有完全理解你。你的意思是广播接收器只有在应用程序打开时才会运行? –

+0

雅...基于你如何声明接收器,它只能在应用程序打开时运行,或者在应用程序未打开时在后台运行。 – Psypher

您可以发送和接收阵列像这样:

发送:

Bundle b=new Bundle(); 
b.putStringArray(key, new String[]{value1, value2}); 
Intent i=new Intent(context, Class); 
i.putExtras(b); 

接收:

Bundle b=this.getIntent().getExtras(); 
String[] array=b.getStringArray(key); 
+0

它没有工作,它说“不幸的是,应用程序已停止” –

+0

我想从boradcast类中的onRecieve方法发送到MedName类中的onCreat方法。 –

我曾面临同样的问题,设置标志,如下为我工作
Intent intent = new Intent(context,MainActivity.class); intent.putStringArrayListExtra("list", arraylistOfStrings); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); 在MainActivity

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

arraylistofStrings被定义为一个ArrayList?我的数组是这样声明的:String [] OurString = null; ......他们是一个错误,因为putStringArrayListExtra第二个参数只接受java.util.ArrayList而我的是java.lang.string [] –

+0

是的它是字符串的ArrayList – praveen

+0

接受如果我的回答是有用的 – praveen