无法解析符号'LocalBroadcastManager'

5
我跟着一个Youtube教程(https://www.youtube.com/watch?v=y8R2C86BIUc&list=PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2)学习,最后一步中导师使用了一个名为LocalBroadcastManager的类。但是这个类不再受支持了...( https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager),因此Android Studio无法编译,出现了错误。
据我所知-我刚接触Android Studio / Java-有一种选项可以将项目迁移到androidx库以使代码运行,但我不知道如何操作。
如果我的问题非常初级,请给我提示,告诉我如何获得基本信息来解决我的问题。
另外,Android Studio正在快速更改/发展,因此我知道那里的解决方案已经过时了。
我尝试了一些stackoverflow上的解决方案,但由于我是一个绝对的初学者,所以不知道在哪里放置什么。

build.gradle (模块:应用程序):

apply plugin: 'com.android.application'


android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.btytcodingwithmitch"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha01'
    }

BluetoothConnectionServer.java:

package com.example.btytcodingwithmitch;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.PrivateKey;
import java.util.UUID;

public class BluetoothConnectionService {

  ...

   private class ConnectedThread extends Thread{

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;


        public void run(){
            byte[] buffer = new byte[1024]; //buffer store for stream

            int bytes; //bytes returned from read

            while (true){
                //read from Input Stream
                try {
                    bytes = mmInStream.read(buffer);
                    String incomingMessage = new String(buffer, 0, bytes);
                    Log.d(TAG, "InputStream" + incomingMessage);

                    Intent incomingMessageIntent = new Intent("incoming Message");
                    incomingMessageIntent.putExtra("theMessage", incomingMessage);

                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);

                } catch (IOException e) {
                    Log.e(TAG, "write: Error reading inputstream" + e.getMessage());
                    break;
                }
            }
        }

错误信息:

"错误: 找不到符号变量 LocalBroadcastManager"


它只在Android组件类中可用。 - KuLdip PaTel
我该如何将这个类/库添加到我的项目中? - KapitaenWurst
1个回答

23
将以下代码添加到您的app/build.gradle文件中,放置在dependencies内: implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' 然后重新构建项目即可完成。
这样就可以实现所需功能。

谢谢!它现在可用了。如果其他人遇到同样的问题,你必须把“implementation'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'”放在依赖项下面。 - KapitaenWurst
是的..我还忘了提到“实现”这一部分。 - DaveAAA
谢谢,它正在工作。实际上,这应该默认添加到androidx.appcompat:appcompat:1.1.0库中,为什么在AndroidX中需要一个特殊的依赖项来管理本地广播? - Waheed Nazir
@KapitaenWurst 仍然有同样的问题。 - Ajay Pandey
在寻找了10个小时之后,这对我起作用了! - Robin Attig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接