前提
Android初心者です。
Android(Kotlin)で、Okhttp3を用いてWebSocketの接続を行うアプリケーションにおいて、バックグラウンド時にサーバーから特定のメッセージが送られてきた場合に、アクティビティを開くアプリケーションを実装したいです。
実現したいこと
- サーバーから特定のメッセージを受信した場合に、Activityを起動する
発生している問題・エラーメッセージ
WebSocketListenerを継承するクラスにおいて、オーバーライドしたonMessage上からActivityを起動したいと考えましたが、Contextが取得できません。 エラー: None of the following functions can be called with the arguments supplied. <init>(Context!, Class<*>!) defined in android.content.Intent <init>(String!, Uri!) defined in android.content.Intent
該当のソースコード
Kotlin
1override fun onMessage(webSocket: WebSocket, text: String) { 2 send("受信:$text") 3 if(text == "テスト") { 4 val dialogIntent = Intent(this, MainActivity::class.java) // ここのthisが取得できない 5 dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 6 startActivity(dialogIntent) 7 } 8 }
試したこと
thisをapplicationに変更するなどは試しましたが、AppCompatActivityを継承していないのでできない気がします。
回答1件
あなたの回答
tips
プレビュー