samedi 5 avril 2014

Java - éléments de ArrayList accès dans méthode onReceive de classe BroadcastReceiver dans android - Stack Overflow


i developing an application where i want to block SMS of User create List.for this purpose i have one an Activity and Second is BroadcastReceiver class. in Activity class i have a function which return the ArrayList,and in BroadcastReceive class i want to access that arrayList,but my code is not access that ArrayList.what is the issue in my code?
Please guide me..
NumberListActivity.java
this is a list class.


public class NumberListActivity extends Activity {
ListView numList1;
Button btnAdd1;
public ArrayList<String> list1 = new ArrayList<String>();
public ArrayAdapter<String> adapter1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_list);
numList1 = (ListView) findViewById(R.id.Smslist);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnAdd);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
numList1.setAdapter(adapter);
}
public ArrayList<String> getArrayList(){
return list;

}

}

SmsLock.java
in this class i want to Access arrayList..


public class SmsLock extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
String phoneNumber;
String senderNum;
NumberListActivity ma = new NumberListActivity();
ArrayList<String> list=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onReceive(Context context, Intent intent) {

final Bundle bundle = intent.getExtras();
try {
adapter=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,list);
Toast.makeText(context, adapter.getCount()+"", Toast.LENGTH_LONG).show();

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
phoneNumber = currentMessage.getDisplayOriginatingAddress();

senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
}

for (int i = 0; i < adapter.getCount(); i++) {
if (senderNum.contains(adapter.getItem(i))) {
abortBroadcast();
}
}

} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);

}
}

}



One way is to access list in SMSLock class is, make list object to static. You can preferred this if your list is not different from instance to instance.




Intent in = getIntent();


ArrayList aList = in.getExtras().getIntegerArrayList("key");



i developing an application where i want to block SMS of User create List.for this purpose i have one an Activity and Second is BroadcastReceiver class. in Activity class i have a function which return the ArrayList,and in BroadcastReceive class i want to access that arrayList,but my code is not access that ArrayList.what is the issue in my code?
Please guide me..
NumberListActivity.java
this is a list class.


public class NumberListActivity extends Activity {
ListView numList1;
Button btnAdd1;
public ArrayList<String> list1 = new ArrayList<String>();
public ArrayAdapter<String> adapter1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_list);
numList1 = (ListView) findViewById(R.id.Smslist);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnAdd);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
numList1.setAdapter(adapter);
}
public ArrayList<String> getArrayList(){
return list;

}

}

SmsLock.java
in this class i want to Access arrayList..


public class SmsLock extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
String phoneNumber;
String senderNum;
NumberListActivity ma = new NumberListActivity();
ArrayList<String> list=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onReceive(Context context, Intent intent) {

final Bundle bundle = intent.getExtras();
try {
adapter=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,list);
Toast.makeText(context, adapter.getCount()+"", Toast.LENGTH_LONG).show();

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
phoneNumber = currentMessage.getDisplayOriginatingAddress();

senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
}

for (int i = 0; i < adapter.getCount(); i++) {
if (senderNum.contains(adapter.getItem(i))) {
abortBroadcast();
}
}

} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);

}
}

}


One way is to access list in SMSLock class is, make list object to static. You can preferred this if your list is not different from instance to instance.



Intent in = getIntent();


ArrayList aList = in.getExtras().getIntegerArrayList("key");


0 commentaires:

Enregistrer un commentaire