Android 在 android 服务中使用 LocalBroadcastManager
Android using LocalBroadcastManager inside android Service
我正在尝试在 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。
要在任何 java class 的烦人方法/内部 class 中获得 outerScope this 你可以调用 WebSocketService.this
LocalBroadcastManager.getInstance(WebSocketService.this).sendBroadcast(broadcastIntent);
或者声明一个私有辅助方法
private WebSocketService getOuterWebSocketService() {
return this;
}
现在您可以在 onMessage 中使用此方法,如下所示
@Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getOuterWebSocketService()).sendBroadcast(broadcastIntent);
}
我正在尝试在 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。
要在任何 java class 的烦人方法/内部 class 中获得 outerScope this 你可以调用 WebSocketService.this
LocalBroadcastManager.getInstance(WebSocketService.this).sendBroadcast(broadcastIntent);
或者声明一个私有辅助方法
private WebSocketService getOuterWebSocketService() {
return this;
}
现在您可以在 onMessage 中使用此方法,如下所示
@Override
public void onMessage(String message) {
Log.i(TAG, "Websocket Received: " + message);
Intent broadcastIntent = new Intent("custom-event");
broadcastIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(getOuterWebSocketService()).sendBroadcast(broadcastIntent);
}