为什么我无法让Google的“从活动中获取结果”起作用?

问题描述 投票:0回答:2

只是刚开始使用Android(以及一般编程)

我想从另一个应用程序接收一个Uri,以便将此Uri提供给Google的Cloud Print。

我正在尝试遵循此处给出的说明:https://developer.android.com/training/basics/intents/result#java

但是我无法在代码的后半部分解析“ PICK_FORM_REQUEST”。

我在做什么错?

这是我的代码:

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// Called when the user taps the Pick button /
public void sendMessage(View view) {
    // Do something in response to button

    final int PICK_FORM_REQUEST = 1;  // The request code
    private void pickForm()
    {
        Intent pickFormIntent = new Intent(Intent.ACTION_PICK);
        pickFormIntent.setType("vnd.android.cursor.dir/vnd.odk.form");
        startActivityForResult(pickFormIntent, PICK_FORM_REQUEST);

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    // Check which request we're responding to
    if (requestCode == PICK_FORM_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The Intent's data URI identifies which form was selected.
            Uri formUri = data.getData();
            // Do something with the form here

            }
        }
    }
}

activity_main_xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp"
    android:ems="10"
    android:hint="@string/edit_message"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:onClick="sendMessage"
    android:text="@string/button_send"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
android uri
2个回答
1
投票

现在您可以尝试

public void sendMessage(View view) {
    // Do something in response to button

    final int PICK_FORM_REQUEST = 1;  // The request code

        Intent pickFormIntent = new Intent(Intent.ACTION_PICK);
        pickFormIntent.setType("vnd.android.cursor.dir/vnd.odk.form");
        startActivityForResult(pickFormIntent, PICK_FORM_REQUEST);
}

0
投票

确定,我设法通过在方法之外声明PICK_FORM_REQUEST来解决它:

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public static final int PICK_FORM_REQUEST = 1;  // The request code
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
© www.soinside.com 2019 - 2024. All rights reserved.