private void displayScanResult(Intent initiatingIntent, String howDataReceived) { //Id String decodedData = initiatingIntent.getStringExtra(Datawedeentity.DATAWEDGE_INTENT_KEY_DATA); //类型 String decodeType = initiatingIntent.getStringExtra(Datawedeentity.DATAWEDGE_INTENT_KEY_DECODER); sendEPC(decodedData.replaceAll("-", "")); }
时间: 2024-03-28 15:40:55 浏览: 58
这段代码看起来像是一个Android应用程序的一个方法,根据传入的意图(Intent)参数和数据接收方式,显示扫描结果。在这个方法中,通过使用getStringExtra()方法获取扫描到的数据和解码器类型,然后使用replaceAll()方法去掉其中的破折号,最后调用sendEPC()方法来处理这个数据。sendEPC()方法的具体实现无从得知,因为这段代码中没有给出sendEPC()方法的定义。
相关问题
用andriod studio写一个通讯录程序包含Activity,事件,数据库,列表组件、Intent、SharedPreference,实现添加删除操作
好的,我可以帮你解答这个问题。
首先,我们需要创建一个新的 Android 项目,并在 MainActivity.java 文件中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private ListView contactsListView;
private ArrayList<Contact> contactsList;
private ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化通讯录列表
contactsListView = findViewById(R.id.contacts_list);
contactsList = new ArrayList<>();
adapter = new ContactAdapter(this, contactsList);
contactsListView.setAdapter(adapter);
// 添加按钮点击事件
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddContactActivity.class);
startActivityForResult(intent, 1);
}
});
// 从数据库中加载通讯录列表
loadContactsFromDatabase();
}
// 从数据库中加载通讯录列表
private void loadContactsFromDatabase() {
SQLiteDatabase db = new ContactDBHelper(this).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM contacts", null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String email = cursor.getString(cursor.getColumnIndex("email"));
Contact contact = new Contact(id, name, phone, email);
contactsList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
adapter.notifyDataSetChanged();
}
// 添加联系人页面返回后的处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String name = data.getStringExtra("name");
String phone = data.getStringExtra("phone");
String email = data.getStringExtra("email");
// 将新的联系人添加到数据库中
SQLiteDatabase db = new ContactDBHelper(this).getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("phone", phone);
values.put("email", email);
db.insert("contacts", null, values);
db.close();
// 将新的联系人添加到通讯录列表中
Contact contact = new Contact(contactsList.size() + 1, name, phone, email);
contactsList.add(contact);
adapter.notifyDataSetChanged();
}
}
}
```
这里我们通过 ListView 和自定义的 ContactAdapter 类来展示通讯录列表,并在页面上添加一个“添加”按钮,点击后跳转到 AddContactActivity 页面,实现添加联系人的功能。
我们还需要创建一个 Contact 类来表示联系人,代码如下:
```
public class Contact {
private int id;
private String name;
private String phone;
private String email;
public Contact(int id, String name, String phone, String email) {
this.id = id;
this.name = name;
this.phone = phone;
this.email = email;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
}
```
接下来,我们需要创建一个 AddContactActivity 类来实现添加联系人的页面,代码如下:
```
public class AddContactActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
// 初始化输入框
nameEditText = findViewById(R.id.name_edit_text);
phoneEditText = findViewById(R.id.phone_edit_text);
emailEditText = findViewById(R.id.email_edit_text);
// 添加按钮点击事件
Button addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
// 返回结果给 MainActivity 处理
Intent intent = new Intent();
intent.putExtra("name", name);
intent.putExtra("phone", phone);
intent.putExtra("email", email);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
这里我们通过 EditText 和一个“添加”按钮来实现输入联系人信息和提交的功能。
最后,我们还需要创建一个 ContactDBHelper 类来管理联系人数据库,代码如下:
```
public class ContactDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
public ContactDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone TEXT, email TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
```
这里我们通过 SQLiteOpenHelper 类来管理联系人数据库,创建一个 contacts 表来存储联系人信息。
至此,我们已经完成了一个简单的通讯录程序的开发,具备了添加、删除联系人的功能。
三、简答题 1、简述如何使用 SharedPreferences 读写数据。 2、简述 Handler 消息机制的工作原理。 3、简述使用HttpURConnection访问网络的步骤。 四、编程题 1、现有两个Activity,分别为UserinfoActivity和ShowActivity,现在要从UserinfoActivity中通过一个“查看”按钮切换界面到ShowActivity,并传递如下三个参数 id=1:username="eric”;state=True,且ShowActivity使用 Toast 显示接收到的参数,请写出两个Activity中的核心代码 2、在网络接口 http://192.168.1.100:8080/news/getnewslist中提供了如下所示的JSON数组格式的返回数据:[f"id":1001,"title”:“"程序员因写代码太乱被杀害","detail”:"凶手是死者同事,维护死者代码时完全看不懂而痛下杀手":"comment" 16359,"image”:"http://192.168.1.100:8080/news/images/6.jpg) 请编写程序,访问该接口获取数据,解析后将数据显示到界面的控件上,同时将数据保存到本地的SQLite 数据库中。
三、简答题
1、使用SharedPreferences读写数据的步骤:
① 获取SharedPreferences对象:SharedPreferences sharedPreferences = getSharedPreferences("name", Context.MODE_PRIVATE);
② 获取SharedPreferences.Editor对象:SharedPreferences.Editor editor = sharedPreferences.edit();
③ 写入数据:editor.putString("key", value);
④ 提交数据:editor.commit();
⑤ 读取数据:sharedPreferences.getString("key", defaultValue);
2、Handler消息机制的工作原理:
Handler是Android中消息机制的基础,它可以实现不同线程之间的通信。Handler包括两个主要部分:消息队列和消息处理器。消息队列是一个存储消息的FIFO队列,消息处理器则是用来处理消息的对象。当Handler创建时,它会自动与当前线程的消息队列关联起来。当调用Handler的sendMessage()方法,会向消息队列中添加一条消息。当Handler处理消息时,会从消息队列中取出一条消息,交给消息处理器处理,如果消息处理器处理完消息后需要更新UI,可以使用Handler的post()或者postDelayed()方法将消息发送到UI线程中执行。
3、使用HttpURLConnection访问网络的步骤:
① 创建URL对象:URL url = new URL("http://www.example.com");
② 打开URLConnection连接:HttpURLConnection connection = (HttpURLConnection) url.openConnection();
③ 设置请求方式:connection.setRequestMethod("GET");
④ 设置连接超时时间:connection.setConnectTimeout(5000);
⑤ 设置读取超时时间:connection.setReadTimeout(5000);
⑥ 发送请求:connection.connect();
⑦ 获取响应码:int responseCode = connection.getResponseCode();
⑧ 获取输入流:InputStream inputStream = connection.getInputStream();
⑨ 读取数据:BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String result = stringBuilder.toString();
⑩ 关闭连接:connection.disconnect();
四、编程题
1、UserinfoActivity中的代码:
```
public void onClick(View view) {
Intent intent = new Intent(UserinfoActivity.this, ShowActivity.class);
intent.putExtra("id", 1);
intent.putExtra("username", "eric");
intent.putExtra("state", true);
startActivity(intent);
}
```
ShowActivity中的代码:
```
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
String username = intent.getStringExtra("username");
boolean state = intent.getBooleanExtra("state", false);
Toast.makeText(ShowActivity.this, "id=" + id + " username=" + username + " state=" + state, Toast.LENGTH_SHORT).show();
```
2、代码如下:
```
public void getData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://192.168.1.100:8080/news/getnewslist");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String result = stringBuilder.toString();
Gson gson = new Gson();
List<News> newsList = gson.fromJson(result, new TypeToken<List<News>>(){}.getType());
for (News news : newsList) {
// 将数据保存到SQLite数据库中
dbHelper.insertNews(news.getId(), news.getTitle(), news.getDetail(), news.getComment(), news.getImage());
}
// 更新UI
Message message = Message.obtain();
message.what = UPDATE_UI;
message.obj = newsList;
handler.sendMessage(message);
reader.close();
inputStream.close();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private void updateUI(List<News> newsList) {
// 将数据显示到界面的控件上
}
// News类
public class News {
private int id;
private String title;
private String detail;
private int comment;
private String image;
public News(int id, String title, String detail, int comment, String image) {
this.id = id;
this.title = title;
this.detail = detail;
this.comment = comment;
this.image = image;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDetail() {
return detail;
}
public int getComment() {
return comment;
}
public String getImage() {
return image;
}
}
// DBHelper类
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "news.db";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "news";
private static final String COLUMN_ID = "id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DETAIL = "detail";
private static final String COLUMN_COMMENT = "comment";
private static final String COLUMN_IMAGE = "image";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_DETAIL + " TEXT, " +
COLUMN_COMMENT + " INTEGER, " +
COLUMN_IMAGE + " TEXT)";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertNews(int id, String title, String detail, int comment, String image) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, id);
values.put(COLUMN_TITLE, title);
values.put(COLUMN_DETAIL, detail);
values.put(COLUMN_COMMENT, comment);
values.put(COLUMN_IMAGE, image);
db.insert(TABLE_NAME, null, values);
db.close();
}
public List<News> getNewsList() {
List<News> newsList = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE));
String detail = cursor.getString(cursor.getColumnIndex(COLUMN_DETAIL));
int comment = cursor.getInt(cursor.getColumnIndex(COLUMN_COMMENT));
String image = cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE));
News news = new News(id, title, detail, comment, image);
newsList.add(news);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return newsList;
}
}
// Handler类
private static final int UPDATE_UI = 1;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
switch (message.what) {
case UPDATE_UI:
List<News> newsList = (List<News>) message.obj;
updateUI(newsList);
break;
}
return false;
}
});
阅读全文