简体   繁体   English

'startActivityForResult(android.content.Intent, int)' 已弃用我该怎么办?

[英]'startActivityForResult(android.content.Intent, int)' is deprecated what can i do?

'startActivityForResult(android.content.Intent, int)' is deprecated what can i do? 'startActivityForResult(android.content.Intent, int)' 已弃用我该怎么办? This is the code for my qr code scanner android app (java) (androidstudio):这是我的二维码扫描器 android 应用程序 (java) (androidstudio) 的代码:

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);
            }
        }
    }
}

A basic training is available at https://developer.android.com/training/basics/intents/result基本培训可在https://developer.android.com/training/basics/intents/result

Here is an example on how to convert the existing code with the new one:以下是有关如何将现有代码转换为新代码的示例:

The old way:旧方法:

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();
    }
}

The new way (Java):新方式(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();
                }
            }
        });

This is Your Code这是你的代码

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);
            }
                }
            }
        });
}

If a function is deprecated, the documentation usually tells you why and what you should use instead :如果不推荐使用 function, 文档通常会告诉您原因以及应该使用什么

This function is deprecated.此 function 已弃用。

This method has been deprecated in favor of using the Activity Result API which brings increased type safety via an ActivityResultContract and the prebuilt contracts for common intents available in androidx.activity.result.contract.ActivityResultContracts , provides hooks for testing, and allow receiving results in separate, testable classes independent from your activity.此方法已被弃用,取而代之的是使用 Activity Result API,它通过ActivityResultContractandroidx.activity.result.contract.ActivityResultContracts中可用的常见意图的预构建合同带来更高的类型安全性,提供用于测试的挂钩,并允许接收结果独立于您的活动的独立的、可测试的类。 Use registerForActivityResult passing in a StartActivityForResult object for the ActivityResultContract .使用registerForActivityResultActivityResultContract传入StartActivityForResult object。

The other answer shows you the developer guide for using this stuff and has an example, but it's important to always look at the documentation when you run into deprecated stuff - it's rare a function is removed with no information, and sometimes you won't need to worry about the deprecation anyway (eg if it only happened with the newest version of Android, meaning they've just decided it will be removed sometime in the future as the older APIs fall out of use)另一个答案向您展示了使用这些东西的开发人员指南并有一个示例,但是当您遇到已弃用的东西时,请务必始终查看文档 - 很少有 function 在没有任何信息的情况下被删除,有时您不需要无论如何都要担心弃用(例如,如果它只发生在最新版本的 Android 上,这意味着他们刚刚决定在将来某个时候将其删除,因为旧的 API 不再使用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;startActivityForResult(android.content.Intent, int)&#39; 已弃用 - 'startActivityForResult(android.content.Intent, int)' is deprecated 什么是 'startIntentSenderForResult(android.content.IntentSender, int, android.content.Intent, int, int, int)' 的替代方案已弃用 - what is alternative of 'startIntentSenderForResult(android.content.IntentSender, int, android.content.Intent, int, int, int)' is deprecated Android-无法解析方法&#39;onActivityResult(int,int,android.content.Intent) - Android - Cannot resolve method 'onActivityResult(int, int, android.content.Intent) &#39;android.support.v4.app.Fragment&#39;中的&#39;startActivity(android.content.Intent,android.os.Bundle)&#39;无法应用于&#39;(android.content.Intent,int)&#39; - 'startActivity(android.content.Intent, android.os.Bundle)' in 'android.support.v4.app.Fragment' cannot be applied to '(android.content.Intent, int)' “MainActivity()”不能应用于“(android.content.Intent)” - 'MainActivity()' cannot be applied to '(android.content.Intent)' 无法解析方法&#39;startActivity(android.content.Intent); - 在Android设备上打开URL - Can't resolve method 'startActivity(android.content.Intent); - Open URL on android device 无法解析方法“isIntentAvailable(android.content.intent)” - Cannot resolve method 'isIntentAvailable(android.content.intent)' 找不到符号方法startActivity(android.content.Intent) - Cannot find symbol method startActivity(android.content.Intent) startActivityForResult(Intent,int)整数参数的目的是什么 - startActivityForResult(Intent, int) what is the purpose of the integer argument 日食中的“意图无法解析为类型”错误,并且在Mac上无法导入“ android.content.intent” - 'Intent cannot be resolved to a type' error in eclipse and cannot import 'android.content.intent' on Mac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM