0
我有一個webView
三個組件來自html
文件。我希望應用在點擊該按鈕後發送短信,並在recyclerView
中顯示消息日誌。如何在recyclerView中管理顯示數據Android
所以短信將被正確發送,但沒有任何顯示在recyclerView
。
message.html資產
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function sendAndroidMessage() {
phoneNumber = document.getElementById('phoneNumber').value;
messageText = document.getElementById('messageText').value;
Android.sendMessage(phoneNumber, messageText);
}
</script>
</head>
<body>
<input id="phoneNumber" type="tel" placeholder="Enter the Phone Number"/>
<br/>
<br/>
<input id="messageText" type="text" placeholder="Enter Message Content" >
<br/>
<br/>
<input id="sendBtn" type="button" value="Send" onclick="sendAndroidMessage()" >
</body>
</html>
javaScriptInterface
public SendMessageInterface(Context context) {
this.context = context;
}
@JavascriptInterface
public void sendMessage(String phoneNumber, String messageText) {
message = new Message();
message.setMessageText(messageText);
message.setPhoneNumber(phoneNumber);
sendSMS();
}
sendSMS():
String smsBody = message.getMessageText();
String phoneNumber = message.getPhoneNumber();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
context.registerReceiver (new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
message.setMessageSent(true);
Toast.makeText(context, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message.setMessageSent(false);
Toast.makeText(context, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message.setMessageSent(false);
Toast.makeText(context, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message.setMessageSent(false);
Toast.makeText(context, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message.setMessageSent(false);
Toast.makeText(context, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, smsBody, sentPI, deliveredPI);
}catch (Exception ex) {
Toast.makeText(context,ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
終於MainActivity.java:
messageInfo = (WebView)findViewById(R.id.message);
messageInfo.loadUrl("file:///android_asset/message.html");
messageInfo.getSettings().setJavaScriptEnabled(true);
message = new Message();
recyclerView = (RecyclerView)findViewById(R.id.show_message);
messageAdapter = new MessageAdapter(this, messageList);
messageInfo.addJavascriptInterface(new SendMessageInterface(MainActivity.this), "Android");
setMessageAdapter();
setMassageAdapter():
messageList.add(message);
recyclerView.setHasFixedSize(false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
我很迷茫,不知道如何解決它或搜索的內容