当我收到短信时,我当前的活动已经结束,并且不知道为什么,我只是用adapter.notifyDataSetChanged刷新我的listview。当我发送消息时我可以刷新它,它只是工作但不是我收到时
这是我的广播接收器
public class SMSReceiver extends BroadcastReceiver {
String from;
String msg;
long time;
PrintMessage pm = new PrintMessage();
@Override
public void onReceive(Context context, Intent intent) {
abortBroadcast();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
// Convertir les PDUs en messages
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
// Enfin, traiter les messages
for (SmsMessage message : messages) {
from = message.getOriginatingAddress();
msg = message.getMessageBody();
time = message.getTimestampMillis();
}
}
pm.printMessage();
}
}
然后我的代码显示消息
public class PrintMessage extends AppCompatActivity implements Telephony.TextBasedSmsColumns {
String idPerso, numero, contact, msg, from;
long time;
Intent intent;
ArrayList<CreateMessageAdapter> list = new ArrayList<>();
ImageButton buttonSend;
EditText textSMS;
Toolbar toolbar;
MessageAdapter adapter;
String[] selection;
Cursor cursor;
TextView tv_toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print_message);
toolbar = (Toolbar) findViewById(R.id.tool_bar_messa);
buttonSend = (ImageButton) findViewById(R.id.btnSend);
textSMS = (EditText) findViewById(R.id.Et_message);
tv_toolbar = (TextView) findViewById(R.id.tv_toolbar);
intent = getIntent();
idPerso = intent.getStringExtra("id");
numero = intent.getStringExtra("numero");
contact = intent.getStringExtra("contact");
selection = new String[]{
Telephony.Sms.THREAD_ID,
Telephony.Sms.BODY,
Telephony.Sms.TYPE,
Telephony.Sms.DATE,
Telephony.Sms.DATE_SENT,
};
printMessage();
setSupportActionBar(toolbar);
if (!contact.equals("no"))
tv_toolbar.setText(contact);
else
tv_toolbar.setText(numero);
tv_toolbar.setMaxWidth(550);
getSupportActionBar().setDisplayShowTitleEnabled(false); //enlever le titre originel
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(numero, null, sms, null, null);
long time = System.currentTimeMillis();
// Cursor cTmp = cr.query(uri, selection, Telephony.Sms.THREAD_ID + "='" + idPerso + "'", null, "date ASC");
// cTmp.moveToFirst();
// int typeTmp = cTmp.getInt(2);
list.add(new CreateMessageAdapter(2, sms, time));
textSMS.setText("");
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
public void printMessage() {
// Read stored sms
Uri uri = Telephony.Sms.CONTENT_URI;
ContentResolver cr = getContentResolver();
cursor = cr.query(uri, selection, Telephony.Sms.THREAD_ID + "='" + idPerso + "'", null, "date ASC");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
int type = cursor.getInt(2);
String body = cursor.getString(1);
long dateNumber = cursor.getLong(3);
long dateNumberSent = cursor.getLong(4);
if (type == 2)
list.add(new CreateMessageAdapter(type, body, dateNumber));
else if (type == 1 || type == 5 || type == 6)
list.add(new CreateMessageAdapter(type, body, dateNumberSent));
cursor.moveToNext();
}
adapter = new MessageAdapter(list, this);
ListView lv = (ListView) findViewById(R.id.lv_message);
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
lv.setStackFromBottom(true);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
Toast.makeText(PrintMessage.this, "long clicked pos: " + pos, Toast.LENGTH_LONG).show();
return true;
}
});
}
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenu.ContextMenuInfo menuInfo) {
}
}
答案 0 :(得分:0)
替换adapter = new MessageAdapter(list, this);
adapter = new MessageAdapter(list, PrintMessage.this);
printMessage()
中的