Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在两个不同的应用程序之间共享数据?_Android_Android Intent - Fatal编程技术网

Android 如何在两个不同的应用程序之间共享数据?

Android 如何在两个不同的应用程序之间共享数据?,android,android-intent,Android,Android Intent,我正在开发我的移动应用程序,我正在尝试将数据(文本)从我的来宾应用程序发送到我的管理员应用程序。这些应用程序是android studio中两个独立的项目,我在应用程序的来宾端收到一个错误,它表示错误:找不到符号变量txt,在我的管理应用程序中,我收到一个不同的错误,说运行'app'时出错:找不到默认活动,我假设这是必须的使用管理应用程序清单文件 这是我为来宾应用程序编写的代码,txt错误来自该应用程序 package com.example.prototype; import android

我正在开发我的移动应用程序,我正在尝试将数据(文本)从我的来宾应用程序发送到我的管理员应用程序。这些应用程序是android studio中两个独立的项目,我在应用程序的来宾端收到一个错误,它表示
错误:找不到符号变量txt
,在我的管理应用程序中,我收到一个不同的错误,说
运行'app'时出错:找不到默认活动
,我假设这是必须的使用管理应用程序清单文件

这是我为来宾应用程序编写的代码,txt错误来自该应用程序

package com.example.prototype;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CleaningService extends AppCompatActivity implements 
View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cleaning_service);
    getSupportActionBar().setTitle("Cleaning Service");

    Button button5 = findViewById(R.id.button5);
    button5.setOnClickListener(this);
}

@Override
public void onClick (View v) {
    switch (v.getId()) {
        case R.id.button5:
            Toast.makeText(this, "Message sent succesfully", Toast.LENGTH_SHORT ).show();
            break;

    }
}


public void sendMessage(View v)
{
    String message;
    message = ((TextView)v).getText().toString();
    Intent i = new Intent();
    i.setAction(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_TEXT, message);
    i.setType("text/plain");
    startActivity(i);
}
}
这是我的管理应用程序项目中的错误。 Androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prototypeadmin">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".GuestTab"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
    <activity
        android:name=".guestrequest"
        android:label="@string/title_activity_guestrequest"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".AdminMenu"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
    <activity
        android:name=".GuestRequests"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <data android:mimeType="text/plain"/>
        </intent-filter>
    </activity>
</application>
其结果应该是,一旦从来宾应用程序发送消息,就会出现在管理应用程序上

文本将从何处发送的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/gradient"
tools:context=".CleaningService">

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="399dp"
    android:layout_height="299dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="280dp"
    android:layout_marginEnd="-4dp"
    android:textColorHint="#FFFFFF">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text here.."
        android:shadowColor="#F8F1F5F4"
        android:textColor="#F8F1F5F4"
        android:textColorHighlight="#F8F1F5F4"
        android:textColorHint="#F1E9E9"
        android:textColorLink="#F8F1F5F4"
        android:textSize="24sp"
        android:textStyle="normal"
        android:id="@+id/et1"/>
</com.google.android.material.textfield.TextInputLayout>

<Button
    android:id="@+id/button5"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="150dp"
    android:layout_marginRight="150dp"
    android:layout_marginBottom="55dp"
    android:text="Send"
    android:textSize="24sp"
    android:onClick="sendMessage"/>

<ImageView
    android:id="@+id/imageView10"
    android:layout_width="161dp"
    android:layout_height="189dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    app:srcCompat="@drawable/mopp" />


您的管理应用程序没有启动器
活动(即:清单中包含ACTION=MAIN和CATEGORY=launcher的应用程序)。这意味着安装应用程序后,您无法启动它。如果不启动,应用程序将保持“停止状态”,Android不会自动启动。这可能就是管理员应用程序无法启动的原因


第二个问题是错误“找不到符号变量txt”

这可能是由
sendMessage()
中的代码引起的:

这里,编译器抱怨
txt
是未知变量。您可能想这样做:

message = ((TextView)v).getText().toString();

在这种情况下,使用变量
v
,该变量是传递到
sendMessage()
方法中的
视图。您需要将
v
转换为
TextView
(我假设它是
TextView
),以便可以对其调用
getText()


我在这里做了一系列假设,包括使用layout.xml中的
onClick
单击按钮调用
sendMessage()

您好,谢谢您的回复。我在清单中添加了action.main和category.launcher。管理应用程序正在启动,我认为这是一个好迹象。尽管我在代码
message=v.getText().toString()中遇到了一个错误。现在它说它找不到getText()方法,您需要将
v
转换为
TextView
。我已经编辑了我的答案你好,大卫。在我的xml中,我现在调用方法
sendMessage()
来调用
getText()
,我想问,我必须在应用程序的管理端还是来宾应用程序上这样做?此外,对于管理应用程序清单文件,我是否只在意图过滤器中保留主活动和启动器活动,还是需要向该字段添加更多活动?我已经更新了密码,你误解了。你对舱单所做的是错误的。您需要一个只包含ACTION=MAIN和CATEGORY=LAUNCHER的
。您还有一个不应该存在的数据条目。此
活动
随后将显示在主屏幕上,您必须手动运行应用程序至少一次,然后您的来宾应用程序才能启动应用程序。由于您的来宾应用程序正在启动带有ACTION=SEND的
Intent
,因此您需要带有ACTION=SEND的
和数据规范,以便在来宾应用程序调用
startActivity()
时运行管理应用程序。
message = txt.getText().toString();
message = ((TextView)v).getText().toString();