【发布时间】:2020-04-18 01:24:34
【问题描述】:
我正在尝试在 android 服务中使用 LocalBroadcastManager。
public class WebSocketService extends Service {
private static final String TAG = "WebSocketService";
private WebSocketClient mWebSocketClient;
public WebSocketService() {
connectWebSocket();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void connectWebSocket() {
URI uri;
try {
uri = new URI("wss://echo.websocket.org");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i(TAG, "Websocket Opened");
mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
}
@Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.i(TAG, "Websocket closed: " + reason);
}
@Override
public void onError(Exception ex) {
Log.i(TAG, "Websocket error");
}
};
}
public void sendMessage(String message) {
mWebSocketClient.send(message);
}
}
创建 websocket 的主要 Activity。
public class MainActivity extends Activity {
private WebSocketService webSocketService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webSocketService = new WebSocketService();
}
public void send(View view) {
webSocketService.sendMessage("socket message");
}
}
及其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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="send"
tools:layout_editor_absoluteX="166dp"
tools:layout_editor_absoluteY="288dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
以上没有异常编译
将LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent); 更改为
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
我得到了错误
Error Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我的问题是是否可以在创建时传递上下文
mWebSocketClient = new WebSocketClient(uri) {
}
这样LocalBroadcastManager可以用还是不用?
一般的问题是,是否有可能将 applicationContext 放入一个令人讨厌的命名空间中。
【问题讨论】:
-
你是怎么开始这个
WebSocketService的?添加代码 -
@ADM 添加了启动 websocket 的代码,一般的问题是我们可以在任何服务/活动类的一个烦人的方法中获取 applicationContext 吗?
-
你可以拥有但你不应该......这会导致内存泄漏和空指针异常,就像在这种情况下......
-
@ADM 正在使用我没有包括在内的 DI,因为那时问题会非常大,这是为了表示目的,主要问题是如何在任何服务的烦人方法中获取 pplicationContext/活动课?
标签: android broadcastreceiver android-service android-context android-broadcast