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

时间:2020-03-04 21:52:42

标签: android android-intent

我正在研究我的移动应用程序,并尝试将数据(文本)从来宾应用程序发送到我的管理应用程序。这些应用是android studio中的两个独立项目,即时通讯在应用程序的来宾端出现错误,提示error: cannot find symbol variable txt,其中与我的管理应用程序一样,我遇到了另一个错误,说Error running 'app': Default Activity not found与管理应用程序清单文件有关。

这是我的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>

管理员应用程序代码:

package com.example.prototypeadmin;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;


public class GuestTab extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tab1, tab2, tab3;
public PageAdapter pageradapter;

TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guest_tab);
    txt = (TextView) findViewById(R.id.txt);

    Intent i = getIntent(); // added object intent to receive data
    String action = i.getAction();
    String type = i.getType();
    if (Intent.ACTION_SEND.equals(action) && type != null) // Shares Data
    {
        if ("text/plain".equals(type))
        {
            String getMessage = i.getStringExtra(Intent.EXTRA_TEXT);
            txt.setText("Guest Request :"+getMessage); // checks validation of string getMessage
        }
    }

    getSupportActionBar().setTitle("Guest Request Panel");



    tabLayout = (TabLayout) findViewById(R.id.tabMode);
    tab1 = (TabItem) findViewById(R.id.Tab1);
    tab2 = (TabItem) findViewById(R.id.Tab2);
    tab3 = (TabItem) findViewById(R.id.Tab3);
    viewPager = findViewById(R.id.viewpager);

    pageradapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(pageradapter);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            if (tab.getPosition() == 0) {
                pageradapter.notifyDataSetChanged();
            } else if (tab.getPosition() == 1) {
                pageradapter.notifyDataSetChanged();
            } else if (tab.getPosition() == 3) {
                pageradapter.notifyDataSetChanged();

            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));



}
}

其结果应该是,从来宾应用程序发送的消息应该出现在管理应用程序上。

要从中发送文本的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" />

1 个答案:

答案 0 :(得分:0)

您的管理应用程序没有启动器Activity(即清单中的ACTION = MAIN和CATEGORY = LAUNCHER的启动器)。这意味着在安装该应用程序后,您将无法启动它。如果您不启动它,则该应用程序将保持“已停止状态”,并且Android不会自动启动它。这可能是管理应用程序无法启动的原因。


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

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

message = txt.getText().toString();

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

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

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

我在这里做了很多假设,包括使用layout.xml中的sendMessage()通过单击按钮调用onClick