首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析符号“LocalBroadcastManager”

无法解析符号“LocalBroadcastManager”
EN

Stack Overflow用户
提问于 2019-07-19 14:49:43
回答 1查看 6.3K关注 0票数 5

我遵循了Youtube教程(https://www.youtube.com/watch?v=y8R2C86BIUc&list=PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2),在最后一步,导师使用了一个名为LocalBroadcastManager的类。但是这个类不再支持了..。(https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager)和Android不会编译,因为这个类/错误。据我所知,-i是androidx/Java的新手--可以选择将项目迁移到androidx-库来运行代码,但我不知道如何运行。

如果我问的是一个绝对初学者的问题,请给我一个提示,在哪里我可以得到基本的信息来解决我的问题。而且,Android的变化/开发速度非常快,因此据我所知,目前的解决方案已经不再是最新的。

我尝试了一些从堆叠溢出解决方案,但我不知道把什么放在哪里,因为我是一个绝对的初学者。

build.gradle (模块: app):

代码语言:javascript
复制
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:

代码语言:javascript
复制
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;
                }
            }
        }

Error-Message

“错误:找不到符号变量LocalBroadcastManager”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-19 15:11:40

将此添加到您的app/build.gradle文件中,在dependencies

implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

然后重建项目

这应该能行

票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57114911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档