"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);
}
}
}
}
基本培训可在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();
}
}
新方法(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();
}
}
});
这是您的代码
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);
}
}
}
});
}
如果函数被弃用,文档通常会告诉您为什么以及应该使用什么:
不赞成使用此函数
此方法已被弃用,取而代之的是使用"活动结果API",该方法通过
ActivityResultContract
和androidx.activity.result.contract.ActivityResultContracts
中提供的用于通用意图的预构建合约提高了类型安全性,提供了用于测试的挂钩,并允许在独立于活动的独立可测试类中接收结果。使用传入StartActivityForResult
对象的registerForActivityResult
作为ActivityResultContract
。
另一个答案向您展示了使用这些东西的开发人员指南,并有一个示例,但当您遇到不推荐使用的东西时,务必查看文档,这很重要-很少有函数在没有信息的情况下被删除,有时你无论如何都不需要担心被弃用(例如,如果它只发生在最新版本的Android上,这意味着他们刚刚决定在未来某个时候将其删除,因为旧的API不再使用)