Using Widget Service SDK
For using the Widget Service SDK you can check the examples.
The required method is connect
for starting a widget connection.
Examples
We have some examples you can try:
Kotlin
package com.example.sampleappkotlin
import ai.jelou.widget.WidgetService
import ai.jelou.widget.WidgetEventListener
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Instance of Widget Service
val widgetService = WidgetService("<apiKey>","<userUniqueId>")
// Change the name of the User
widgetService.name = "<userNames>"
// Initialize Widget Service Connection
widgetService.connect(this@MainActivity)
// Attach Function to execute when new message arrives
widgetService.on("message", WidgetEventListener { data ->
// Here Goes Your Code
println("New Message Data:$data")
})
}
}
Android(Java)
package com.example.sampleapp;
import ai.jelou.widget.WidgetService;
import ai.jelou.widget.WidgetEventListener;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WidgetService widgetService = new WidgetService("<apiKey>, "<userUniqueId>");
widgetService.setName("<userNames>");
widgetService.connect(MainActivity.this);
try {
widgetService.on("message", new WidgetEventListener() {
@Override
public void run(String data) {
// Here Goes Your Code
System.out.println("New Message Data:"+ data);
}
});
}catch(Exception e) {
// Here Goes Error Handling
System.out.println("Error on attaching event" + e.toString());
}
}
}
Last updated