String smsBody = smsCursor.getString(smsCursor.getColumnIndexOrThrow("body")); smsList.add(smsBody);改成读取电话号码和短信内容
时间: 2024-04-01 21:36:35 浏览: 20
如果您要读取短信的电话号码和短信内容,可以将`smsBody`替换为`smsAddress`和`smsBody`,如下所示:
```
String smsAddress = smsCursor.getString(smsCursor.getColumnIndexOrThrow("address"));
String smsBody = smsCursor.getString(smsCursor.getColumnIndexOrThrow("body"));
smsList.add(smsAddress + ": " + smsBody);
```
这样,您将读取短信的电话号码和短信内容,并将它们作为字符串添加到`smsList`列表中。请注意,`"address"`和`"body"`是短信数据库中相应字段的名称。
相关问题
安卓写一个读取系统短信并输出一个列表的功能
你可以按照以下步骤实现:
1. 添加读取短信的权限到 AndroidManifest.xml 文件中:
```xml
<uses-permission android:name="android.permission.READ_SMS" />
```
2. 在你的 Activity 中创建一个 ArrayList 来存储短信:
```java
ArrayList<String> smsList = new ArrayList<>();
```
3. 在 onCreate() 方法中,使用 ContentResolver 来读取短信:
```java
ContentResolver contentResolver = getContentResolver();
Cursor smsCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (smsCursor != null) {
while (smsCursor.moveToNext()) {
String smsBody = smsCursor.getString(smsCursor.getColumnIndexOrThrow("body"));
smsList.add(smsBody);
}
smsCursor.close();
}
```
4. 创建一个 ListView 并将短信列表设置为其 Adapter:
```java
ListView listView = findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsList);
listView.setAdapter(adapter);
```
完整代码如下:
```java
public class MainActivity extends AppCompatActivity {
ArrayList<String> smsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver contentResolver = getContentResolver();
Cursor smsCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
if (smsCursor != null) {
while (smsCursor.moveToNext()) {
String smsBody = smsCursor.getString(smsCursor.getColumnIndexOrThrow("body"));
smsList.add(smsBody);
}
smsCursor.close();
}
ListView listView = findViewById(R.id.list_view);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsList);
listView.setAdapter(adapter);
}
}
```
注意:在 Android 6.0 及以上版本中,需要动态请求读取短信权限。
用jsp写一个评论网页
好的,下面是一个简单的 JSP 代码,用于实现一个评论网页:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>评论网页</title>
</head>
<body>
<h1>评论区</h1>
<form action="addComment.jsp" method="post">
<label>昵称:</label>
<input type="text" name="nickname"><br>
<label>评论:</label>
<textarea name="comment" cols="30" rows="5"></textarea><br>
<input type="submit" value="提交评论">
</form>
<hr>
<h2>全部评论</h2>
<%-- 从数据库中读取评论并展示 --%>
<%
// 假设数据库表名为 comment,包含 id、nickname 和 content 三个字段
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dbname";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM comment");
while (rs.next()) {
String nickname = rs.getString("nickname");
String content = rs.getString("content");
out.println("<p><strong>" + nickname + ":</strong> " + content + "</p>");
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
```
此代码实现了一个简单的评论网页,包括一个表单用于提交评论,以及一个展示全部评论的区域。用户输入评论后,将会把数据传递给 addComment.jsp 页面进行处理,将评论保存到数据库中。在展示评论时,从数据库中读取评论数据并逐条展示。需要注意的是,此代码仅供参考,需要根据实际需求进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)