Net*_*ull 2 java android qr-code onactivityresult
'startActivityForResult(android.content.Intent, int)' 已弃用,我该怎么办?这是我的二维码扫描仪 Android 应用程序 (Java) 的代码:
package com.example.wfr;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qr_scanner_layout);
//Button click event to open QR scanner
findViewById(R.id.camera_button).setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, QRCodeScanner.class);
intent.putExtra("SCAN_FORMATS", "QR_CODE");
startActivityForResult(intent, REQUEST_CODE);
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
if (data != null) {
String scannedText = data.getStringExtra("com.journeyapps.barcodescanner.CaptureActivity.SCAN_RESULT");
TextView scannedTextView = findViewById(R.id.scanned_text);
scannedTextView.setText(scannedText);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本培训可在https://developer.android.com/training/basics/intents/result上获得
以下是如何将现有代码转换为新代码的示例:
老办法:
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
startActivityForResult(intent, 123);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 123) {
doSomeOperations();
}
}
Run Code Online (Sandbox Code Playgroud)
新方法(Java):
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
someActivityResultLauncher.launch(intent);
}
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
doSomeOperations();
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是你的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qr_scanner_layout);
//Button click event to open QR scanner
findViewById(R.id.camera_button).setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, QRCodeScanner.class);
intent.putExtra("SCAN_FORMATS", "QR_CODE");
someActivityResultLauncher.launch(intent);
});
}
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
if (data != null) {
String scannedText = data.getStringExtra("com.journeyapps.barcodescanner.CaptureActivity.SCAN_RESULT");
TextView scannedTextView = findViewById(R.id.scanned_text);
scannedTextView.setText(scannedText);
}
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4795 次 |
| 最近记录: |