Invoke Flutter (Dart) code from native Android home screen widget - android

I added a native Android home screen widget to my Flutter application.
In my AppWidgetProvider implementation, I'd like to call dart code in my onUpdate() method using a platform channel.
Is this possible? If so, how can this be achieved?
My current Android (Java) code:
package com.westy92.checkiday;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.util.Log;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterNativeView;
public class HomeScreenWidget extends AppWidgetProvider {
private static final String TAG = "HomeScreenWidget";
private static final String CHANNEL = "com.westy92.checkiday/widget";
private static FlutterNativeView backgroundFlutterView = null;
private static MethodChannel channel = null;
#Override
public void onEnabled(Context context) {
Log.i(TAG, "onEnabled!");
backgroundFlutterView = new FlutterNativeView(context, true);
channel = new MethodChannel(backgroundFlutterView, CHANNEL);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i(TAG, "onUpdate!");
if (channel != null) {
Log.i(TAG, "channel not null, invoking dart method!");
channel.invokeMethod("foo", "extraJunk");
Log.i(TAG, "after invoke dart method!");
}
}
}
Dart code:
void main() {
runApp(Checkiday());
}
class Checkiday extends StatefulWidget {
#override
_CheckidayState createState() => _CheckidayState();
}
class _CheckidayState extends State<Checkiday> {
static const MethodChannel platform = MethodChannel('com.westy92.checkiday/widget');
#override
void initState() {
super.initState();
platform.setMethodCallHandler(nativeMethodCallHandler);
}
Future<dynamic> nativeMethodCallHandler(MethodCall methodCall) async {
print('Native call!');
switch (methodCall.method) {
case 'foo':
return 'some string';
default:
// todo - throw not implemented
}
}
#override
Widget build(BuildContext context) {
// ...
}
}
When I add the widget to my home screen, I see:
I/HomeScreenWidget(10999): onEnabled!
I/HomeScreenWidget(10999): onUpdate!
I/HomeScreenWidget(10999): channel not null, invoking dart method!
I/HomeScreenWidget(10999): after invoke dart method!
However, my dart code does not seem to be receiving the invocation.

I also needed some native android widgets to communicate with my dart code and after some tinkering I managed to do this. In my opinion the documentation on how to do this is a bit sparse but with a bit of creativity I managed to get this to work. I haven't done enough testing to call this 100% production ready, but it seems to be working...
Dart setup
Go to main.dart and add the following top-level function:
void initializeAndroidWidgets() {
if (Platform.isAndroid) {
// Intialize flutter
WidgetsFlutterBinding.ensureInitialized();
const MethodChannel channel = MethodChannel('com.example.app/widget');
final CallbackHandle callback = PluginUtilities.getCallbackHandle(onWidgetUpdate);
final handle = callback.toRawHandle();
channel.invokeMethod('initialize', handle);
}
}
then call this function before running your app
void main() {
initializeAndroidWidgets();
runApp(MyApp());
}
this will ensure that we can get a callback handle on the native side for our entry point.
Now add an entry point like so:
void onWidgetUpdate() {
// Intialize flutter
WidgetsFlutterBinding.ensureInitialized();
const MethodChannel channel = MethodChannel('com.example.app/widget');
// If you use dependency injection you will need to inject
// your objects before using them.
channel.setMethodCallHandler(
(call) async {
final id = call.arguments;
print('on Dart ${call.method}!');
// Do your stuff here...
final result = Random().nextDouble();
return {
// Pass back the id of the widget so we can
// update it later
'id': id,
// Some data
'value': result,
};
},
);
}
This function will be the entry point for our widgets and gets called when our widgets onUpdate method is called. We can then pass back some data (for example after calling an api).
Android setup
The samples here are in Kotlin but should work with some minor adjustments also in Java.
Create a WidgetHelper class that will help us in storing and getting a handle to our entry point:
class WidgetHelper {
companion object {
private const val WIDGET_PREFERENCES_KEY = "widget_preferences"
private const val WIDGET_HANDLE_KEY = "handle"
const val CHANNEL = "com.example.app/widget"
const val NO_HANDLE = -1L
fun setHandle(context: Context, handle: Long) {
context.getSharedPreferences(
WIDGET_PREFERENCES_KEY,
Context.MODE_PRIVATE
).edit().apply {
putLong(WIDGET_HANDLE_KEY, handle)
apply()
}
}
fun getRawHandle(context: Context): Long {
return context.getSharedPreferences(
WIDGET_PREFERENCES_KEY,
Context.MODE_PRIVATE
).getLong(WIDGET_HANDLE_KEY, NO_HANDLE)
}
}
}
Replace your MainActivity with this:
class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler {
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, WidgetHelper.CHANNEL)
channel.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"initialize" -> {
if (call.arguments == null) return
WidgetHelper.setHandle(this, call.arguments as Long)
}
}
}
}
This will simply ensure that we store the handle (the hash of the entry point) to SharedPreferences to be able to retrieve it later in the widget.
Now modify your AppWidgetProvider to look something similar to this:
class Foo : AppWidgetProvider(), MethodChannel.Result {
private val TAG = this::class.java.simpleName
companion object {
private var channel: MethodChannel? = null;
}
private lateinit var context: Context
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
this.context = context
initializeFlutter()
for (appWidgetId in appWidgetIds) {
updateWidget("onUpdate ${Math.random()}", appWidgetId, context)
// Pass over the id so we can update it later...
channel?.invokeMethod("update", appWidgetId, this)
}
}
private fun initializeFlutter() {
if (channel == null) {
FlutterMain.startInitialization(context)
FlutterMain.ensureInitializationComplete(context, arrayOf())
val handle = WidgetHelper.getRawHandle(context)
if (handle == WidgetHelper.NO_HANDLE) {
Log.w(TAG, "Couldn't update widget because there is no handle stored!")
return
}
val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(handle)
// Instantiate a FlutterEngine.
val engine = FlutterEngine(context.applicationContext)
val callback = DartExecutor.DartCallback(context.assets, loader.findAppBundlePath(), callbackInfo)
engine.dartExecutor.executeDartCallback(callback)
channel = MethodChannel(engine.dartExecutor.binaryMessenger, WidgetHelper.CHANNEL)
}
}
override fun success(result: Any?) {
Log.d(TAG, "success $result")
val args = result as HashMap<*, *>
val id = args["id"] as Int
val value = args["value"] as Int
updateWidget("onDart $value", id, context)
}
override fun notImplemented() {
Log.d(TAG, "notImplemented")
}
override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
Log.d(TAG, "onError $errorCode")
}
override fun onDisabled(context: Context?) {
super.onDisabled(context)
channel = null
}
}
internal fun updateWidget(text: String, id: Int, context: Context) {
val views = RemoteViews(context.packageName, R.layout.small_widget).apply {
setTextViewText(R.id.appwidget_text, text)
}
val manager = AppWidgetManager.getInstance(context)
manager.updateAppWidget(id, views)
}
The important thing here is initializeFlutter that will make sure we can get a handle to our entry point. In onUpdate we are then calling channel?.invokeMethod("update", appWidgetId, this) that will trigger the callback in our MethodChannel on the dart side defined earlier. Then we handle the result later in success (at least when the call is successful).
Hopefully this will give you a rough idea on how to achieve this...

First, please ensure that you are invoking FlutterMain.startInitialization() and then FlutterMain.ensureInitializationComplete() before attempting to execute any Dart code. These calls are necessary to bootstrap Flutter.
Second, can you try this same goal using the new experimental Android embedding?
Here is a guide for executing Dart code using the new embedding:
https://github.com/flutter/flutter/wiki/Experimental:-Reuse-FlutterEngine-across-screens
If your code still doesn't work as expected with the new Android embedding then it should be easier to debug what the problem is. Please post back with success, or any new error information.

You need to pass the getFlutterView() from the MainActivity instead of creating a new BackgroundFlutterView:
channel = new MethodChannel(MainActivity.This.getFlutterView(), CHANNEL);
"This" being like:
public class MainActivity extends FlutterActivity {
public static MainActivity This;
#Override
protected void onCreate(Bundle savedInstanceState) {
This = this;
...
}

maybe you can use invokeMethod(String method, #Nullable Object arguments, MethodChannel.Result callback) and use callback to get the fail reason.

FlutterMain is deprecated, use FlutterLoader.
For example (kotlin)
val loader = FlutterLoader()
loader?.startInitialization(context!!)
loader?.ensureInitializationComplete(context!!, arrayOf())
Another thing, when app is in background and you want to communicate with parent app, you need to initialize method channel again, initial initialization from onUpdate won't work then. In that case code in flutter part will be executed in separate isolate.

Related

How to access MethodChannel of Flutter engine created with FlutterActivity.withNewEngine()?

I am trying to create a Android app which launches flutter inside it. I have learnt that we can pass data to flutter via MethodChannels like this:
MethodChannel(
FlutterEngineCache.getInstance().get(NEARBY_PLACES_ENGINE_KEY)?.dartExecutor?.binaryMessenger,
"APP_CHANNEL"
).setMethodCallHandler { call, result ->
if (call.method == "getGreetings") {
val coordinates = "Hello"
result.success(coordinates)
}
}
startActivity(FlutterActivity
.withCachedEngine(NEARBY_PLACES_ENGINE_KEY)
.build(this))
This is possible when I have access to FlutterEngine instance (In this case with help of FlutterEngineCache). But how do we get the BinaryMessenger of a engine created with FlutterActivity.withNewEngine() ? Please help. TIA!
Okay so I found the way to do it.
Created a custom FlutterActivity class and added that in the manifest instead of the one from io.flutter.embedding.android.FlutterActivity. The custom FlutterActivity extends from FlutterActivity, and overrides some methods to be able to access the newEngine.
class CustomFlutterActivity : FlutterActivity() {
companion object {
var methodChannelInvoker: (MethodCall, Result) -> Unit = { _, _ -> }
fun withCachedEngine(cachedEngineId: String): CachedEngineIntentBuilder {
return CachedEngineIntentBuilder(CompassFlutterActivity::class.java, cachedEngineId)
}
fun withNewEngine(): NewEngineIntentBuilder {
return NewEngineIntentBuilder(CompassFlutterActivity::class.java)
}
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "APP_CHANNEL")
.setMethodCallHandler { call, result ->
methodChannelInvoker(call, result)
}
}
}
and at the launch site,
CustomFlutterActivity.methodChannelInvoker = { call, result ->
if (call.method == "getGreetings") {
val greetings = "Hello there!"
result.success(coordinates)
}
}
startActivity(CustomFlutterActivity
.withNewEngine()
.initialRoute("/custom_route")
.build(this))
This way, when the new engine launches, you get callback at configureFlutterEngine with new engine as parameter, we can simply access dartExecutor at that point.
Oh yes, and don't forget to add the CustomFlutterActivity in the manifest instead of FlutterActivity.

Does Kotlin `by` keyword work for nullable delegate?

I've pretty excited by Kotlin compiler features and by by in particular - it saves time generating gelegating code:
https://kotlinlang.org/docs/reference/delegation.html
But i want delegate to be nullable and delegating code to check if it's null first and return if it is:
interface Base {
val message: String
fun print()
}
class BaseImpl(val x: Int?) : Base {
override val message = "BaseImpl: x = $x"
override fun print() { println(message) }
}
class Derived(b: Base?) : Base by b {
// This property is not accessed from b's implementation of `print`
override val message = "Message of Derived"
}
fun main() {
val b = BaseImpl(10)
val derived = Derived(b)
derived.print()
println(derived.message)
}
When compiling ^ i'm getting Type mismatch: inferred type is Base? but Base was expected.
Is it still possible with Kotlin?
To be more detailed i'd like Kotlin compiler to generate forwarding calls to wrapped impl (extWebChromeClient) in https://developer.android.com/reference/android/webkit/WebChromeClient like follows:
private WebChromeClient intWebChromeClient = new WebChromeClient()
{
#Override
public void onReceivedTitle(WebView view, String title)
{
if (extWebChromeClient != null)
{
extWebChromeClient.onReceivedTitle(view, title);
}
}
...
You can make this yourself using dynamic proxies, though I wouldn't really recommend it. Note that for non-void methods there's no way to require overriding them. The below implementation just throws exceptions for them unconditionally, but you could still call them for non-null x.
inline fun <reified T : Any> nullableProxy(x: T?): T {
val handler = InvocationHandler { _, method, args ->
if (method.returnType == Void.TYPE) {
if (x != null) {
method.invoke(x, *(args ?: arrayOf()))
}
} else
throw UnsupportedOperationException("Non-void method")
}
return Proxy.newProxyInstance(
T::class.java.classLoader,
arrayOf(T::class.java),
handler) as T
}
class Derived(b: Base?) : Base by nullableProxy(b)
This also won't perform as well as implementing methods directly would.

How to implement Java step Builder pattern in Kotlin

In my current Android project I am investigating the use of Kotlin.
I am rewriting a 100% Java Android app to 100% Kotlin.
I'm stuck with attempting to implement my Java step builders though.
I employed Java step Builders as they force users of my code to supply all required data and/or functions before being able to execute an associated RxJava process.
These RxJava processes are complex and I wished to simplify their initialisation and execution as much as possible.
The use of Java Step builders allow developers to write the following code:-
Sequence.builder()
.stepOne(one)
.stepTwo(two)
.stepThree(three)
.build()
.execute();
What I am looking for is a Kotlin version of this approach.
My initial thought was that Kotlin would support Builders and Step Builders.
I am not "precious" about employing Builders in Kotlin, the Kotlin solution must force the developers that use my code to have supplied all required data and/or functions before they are able to execute the associated "executed" code.
From investigating Kotlin I've discovered internal DSLs which are sounding both interesting topic in themselves and a possible solution to this particular question.
I have a number of Step Builders to implement, none of these have more than 6 parameters. I do like to try and keep to SOLID no more than three parameters rule though.
Also if it makes any difference, some of the passed parameters are RxJava Actions and Consumers. Default values are not relevant here as none of the parameters have viable default values.
UPDATE
My Java step builders all resemble this:-
public class ExampleSequence extends Sequence {
private static final String TAG = "ExampleSequence";
private final Action onComplete;
private final Consumer<? super Throwable> onError;
/**
* #param builder
*/
private ExampleSequence(final Builder builder) {
super(builder.getDoLoginRefreshFail());
this.onError = builder.getOnError();
this.onComplete = builder.getOnComplete();
}
/**
*
*/
public static OnCompleteAction builder() {
return new Builder();
}
public interface OnCompleteAction {
onErrorAction onComplete(#NonNull final Action onComplete);
}
public interface onErrorAction {
DoLoginRefreshFail onError(#NonNull final Consumer<? super Throwable> onError);
}
public interface DoLoginRefreshFail {
Build doLoginRefreshFail(#NonNull final Action doLoginRefreshFail);
}
public interface Build {
ExampleSequence build();
}
#SuppressLint("CheckResult")
public void execute() {
final AtomicInteger retryCounter = new AtomicInteger(0);
final Observable<Response<GraphqlQueryResponse>> feedArticles = getPageAndNextInboxArticles(offset, limit)
.onErrorResumeNext(manufactureResumeNext())
.subscribeOn(Schedulers.io());
final Observable<Response<GraphqlQueryResponse>> readingListArticles = getPageAndReadingListArticles(readingListoffset, limit)
.onErrorResumeNext(manufactureResumeNext())
.subscribeOn(Schedulers.io());
login()
.flatMap(...)
.ignoreElement()
.andThen(...)
.andThen(...)
.ignoreElements()
.andThen(...)
.flattenAsObservable(x -> x)
.flatMapCompletable(...)
.retryWhen(errors -> errors.flatMap(e -> constructRetryHandler(retryCounter, e)))
.doOnComplete(onComplete)
.doOnError(onError)
.doAfterTerminate(doAfterTerminate())
.doOnSubscribe(compositeDisposable::add)
.blockingAwait();
}
/**********************************************************************************
*
* BUILDER
*
*/
public static class Builder implements OnCompleteAction, onErrorAction, DoLoginRefreshFail, Build {
private Action onComplete;
private Consumer<? super Throwable> onError;
private Action doLoginRefreshFail;
/***********************************************************************
*
*/
#Override
public ExampleSequence build() {
return new ExampleSequence(this);
}
#Override
public onErrorAction onComplete(#NonNull final Action onComplete) {
this.onComplete = onComplete;
return this;
}
#Override
public DoLoginRefreshFail onError(#NonNull final Consumer<? super Throwable> onError) {
this.onError = onError;
return this;
}
#Override
public Build doLoginRefreshFail(#NonNull final Action doLoginRefreshFail) {
this.doLoginRefreshFail = doLoginRefreshFail;
return this;
}
/**
* #return the onError
*/
Consumer<? super Throwable> getOnError() {
return onError;
}
/**
* #return the onComplete
*/
Action getOnComplete() {
return onComplete;
}
Action getDoLoginRefreshFail() {
return doLoginRefreshFail;
}
}
}
The step builder pattern in Kotlin is completely doable, and I've provided an example of it that mirrors the Java example you provided.
class ExampleSequence private constructor(builder: Builder): Sequence(builder.doLoginRefreshFail) { //This is your "super()" call.
//This is equivalent to assigning the final variables [onComplete] and [onError] in the class constructor
private val onComplete = builder.onComplete
private val onError = builder.onError
//More info about companion objects here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects
companion object {
//Java will see this as [ExampleSequence.Companion.builder()] unless you add this annotation
#JvmStatic
fun builder(): OnCompleteAction = Builder()
}
fun execute() {
//Do your stuff here...
}
//The following classes and interfaces are similar to being static inner classes. If you want the classes to access
//fields of the enclosing outer class, you must use the keyword [inner] before declaring the class. Example:
// inner class Foo { ... }
interface OnCompleteAction {
fun onComplete(onComplete: Action): onErrorAction
}
interface DoLoginRefreshFail {
fun doLoginRefreshFail(doLoginRefreshFail: Action): Build
}
interface onErrorAction {
fun onError(onError: Consumer<in Throwable>): DoLoginRefreshFail //The [in] keyword is the same as saying Consumer<? super Throwable>
}
interface Build {
fun build(): ExampleSequence
}
class Builder: OnCompleteAction, onErrorAction, DoLoginRefreshFail, Build {
//The [lateinit] keyword states that this variable will be initialized later. Calling it before it is initialized will throw an exception
lateinit var onComplete: Action
private set //Only this class can modify.
lateinit var onError: Consumer<in Throwable>
private set
lateinit var doLoginRefreshFail: Action
private set
//No special differences here... oooh, inlined [override] keyword!
override fun onComplete(onComplete: Action): onErrorAction {
this.onComplete = onComplete
return this
}
override fun doLoginRefreshFail(doLoginRefreshFail: Action): Build {
this.doLoginRefreshFail = doLoginRefreshFail
return this
}
override fun onError(onError: Consumer<in Throwable>): DoLoginRefreshFail {
this.onError = onError
return this
}
override fun build(): ExampleSequence = ExampleSequence(this)
//Where are the getter methods? If you look at the variable declarations, they are public by default.
//This means that these variables are public read, but can only be set by this class only. In other words, built-in getter!
}
}
However, in a pure Kotlin project, step builder is sort of an anti-pattern. With default and named parameters built into the language, you can actually achieve SOLID by a simple data class. Taking the ExampleSequence class for example, your solution could look something like:
data class ExampleSequence(
private val onComplete: Action,
private val onError: Consumer<in Throwable>,
private val doLoginRefreshFail: Action,
private val aNewParam: String = "Default")
: Sequence(doLoginRefreshFail) { //This is your "super()" call.
fun execute() {
//Do your stuff here...
}
}
fun foo() {
//Example of using named parameters and passing in variables. Notice parameters aren't in the same order as how it is declared in the class
ExampleSequence(
onError = Consumer(),
onComplete = Action(),
doLoginRefreshFail = Action()
).execute()
//Since I added [aNewParam], instead of using the default, let's change it.
ExampleSequence(
onError = Consumer(),
onComplete = Action(),
doLoginRefreshFail = Action(),
aNewParam = "Something else!"
).execute()
}
Here is a nice article going into a bit more detail: https://dev.to/chrisvasqm/avoiding-the-builder-design-pattern-in-kotlin-3b1a
Also, in case you need another example of a step builder pattern in Kotlin, you might want to check this out too: https://www.baeldung.com/kotlin-builder-pattern

Explain this basic Kotlin function

I'm new to Java/Kotlin and am working through this tutorial to build an Android app with a widget that can query some JSON from a URL and render said results.
I am confused by the last code example:
val service = ServiceVolley()
val apiController = APIController(service)
val path = "example_endpoint"
val params = JSONObject()
params.put("email", "foo#email.com")
params.put("password", "barpass")
apiController.post(path, params) { response ->
// Parse the result
}
As usual in Kotlin, if the last parameter to a function is a function (and you're passing a lambda expression as the corresponding argument), you can specify it outside of parentheses, as we’ve done above — one of the small quirks I love about Kotlin.
In my widget code I have a helper function updateAppWidget in which I use the above code, and can sucessfully query the API, however I have ended up with the bulk of the code which was in the updateAppWidget function inside the { response -> // Parse the result } block:
apiController.post(path,params) { response ->
// Get 'bar' from the response which is {'foo':'bar'}
val widgetText = response?.get(response.names().getString(0)).toString()
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.statusr)
views.setTextViewText(R.id.appwidget_text, widgetText)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
Can someone explain the significance of the last 3 lines of the first code block, and tell me how I might write this to bring the logic up one level, and whether this is worth while?
The immediate problem I notice is that I can't reference widgetText outwith this block.
EDIT for clarity
I think I'm in over my head. Further reading suggests that I'm passing a lambda by using ->??? I guess what I really want to do is:
Get the call to apiController.post out of the widget code completely, so I have that now in a separate class:
class GetData {
fun widget_text(){
val service = ServiceVolley()
val apiController = APIController(service)
val path = "endpoint"
val params = JSONObject()
params.put("some", "data")
apiController.post(path, params) { response ->
val widgetText = response?.get(response.names().getString(0)).toString()
}
}
}
Would like to be able to call something like GetData.widget_text() from within updateAppWidget but I'm back to my original problem: how do I make widgetText available outside apiController.post(path,params) { response -> // Logic }} and return this.
The meaning of the first last three lines: the data in params are passed to some type of backend (server).
apiController.post(path, params) { response ->
val widgetText = response?.get(response.names().getString(0)).toString()
// Display the result in the App Widget
}
The request is executed asynchronously. This means, the code in the lambda expression will run after the response from the server comes in, while the UI of the app will remain clickable. The method which started the backend call, will have finished (if it had to wait until the response came in the UI could freeze).
A possible app structure using GetData as the class which manages the backend call:
class GetData {
interface WidgetTextCallback {
fun onTextLoaded(text: String)
}
companion object {
fun widget_text(callback: WidgetTextCallback) {
val service = ServiceVolley()
val apiController = APIController(service)
val path = "endpoint"
val params = JSONObject()
params.put("some", "data")
apiController.post(path, params) { response ->
val widgetText = response?.get(response.names().getString(0)).toString()
callback.onTextLoaded(widgetText)
}
}
}
}
And use the interface to retrieve the widget text:
class NewAppWidget : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
GetData.widget_text(object: GetData.WidgetTextCallback{
override fun onTextLoaded(widgetText: String) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, widgetText, appWidgetManager, appWidgetId)
}
}
})
}
companion object {
internal fun updateAppWidget(context: Context, widgetText: String, appWidgetManager: AppWidgetManager,
appWidgetId: Int) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.new_app_widget)
views.setTextViewText(R.id.appwidget_text, widgetText)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
"bring this logic up one level" sounds like you want to handle the response somewhere else in your code. Why don't you just pass it to a handler function?
fun handleResponse(response: Type?) {
// Parse the result
}
apiController.post(path, params) { response ->
handleResponse(response)
}
or shorter:
apiController.post(path, params) { handleResponse(it) }

Android equivalent to NSNotificationCenter

In the process of porting an iPhone application over to android, I am looking for the best way to communicate within the app. Intents seem to be the way to go, is this the best (only) option? NSUserDefaults seems much lighter weight than Intents do in both performance and coding.
I should also add I have an application subclass for state, but I need to make another activity aware of an event.
The best equivalent I found is LocalBroadcastManager which is part of the Android Support Package.
From the LocalBroadcastManager documentation:
Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
It is more efficient than sending a global broadcast through the system.
When using this, you can say that an Intent is an equivalent to an NSNotification. Here is an example:
ReceiverActivity.java
An activity that watches for notifications for the event named "custom-event-name".
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// This is just like [[NSNotificationCenter defaultCenter] addObserver:...]
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
// This is somewhat like [[NSNotificationCenter defaultCenter] removeObserver:name:object:]
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
SenderActivity.java
The second activity that sends/broadcasts notifications.
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Every time a button is clicked, we want to broadcast a notification.
findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage();
}
});
}
// Send an Intent with an action named "custom-event-name". The Intent sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
With the code above, every time the button R.id.button_send is clicked, an Intent is broadcasted and is received by mMessageReceiver in ReceiverActivity.
The debug output should look like this:
01-16 10:35:42.413: D/sender(356): Broadcasting message
01-16 10:35:42.421: D/receiver(356): Got message: This is my message!
Here is something similar to #Shiki answer, but from the angle of iOS developers and Notification center.
First create some kind of NotificationCenter service:
public class NotificationCenter {
public static void addObserver(Context context, NotificationType notification, BroadcastReceiver responseHandler) {
LocalBroadcastManager.getInstance(context).registerReceiver(responseHandler, new IntentFilter(notification.name()));
}
public static void removeObserver(Context context, BroadcastReceiver responseHandler) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(responseHandler);
}
public static void postNotification(Context context, NotificationType notification, HashMap<String, String> params) {
Intent intent = new Intent(notification.name());
// insert parameters if needed
for(Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
intent.putExtra(key, value);
}
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
Then, you will also need some enum type to be secure of mistakes in coding with strings - (NotificationType):
public enum NotificationType {
LoginResponse;
// Others
}
Here is usage(add/remove observers) for example in activities:
public class LoginActivity extends AppCompatActivity{
private BroadcastReceiver loginResponseReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do what you need to do with parameters that you sent with notification
//here is example how to get parameter "isSuccess" that is sent with notification
Boolean result = Boolean.valueOf(intent.getStringExtra("isSuccess"));
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//subscribe to notifications listener in onCreate of activity
NotificationCenter.addObserver(this, NotificationType.LoginResponse, loginResponseReceiver);
}
#Override
protected void onDestroy() {
// Don't forget to unsubscribe from notifications listener
NotificationCenter.removeObserver(this, loginResponseReceiver);
super.onDestroy();
}
}
and here is finally how we post notification to NotificationCenter from some callback or rest service or whatever:
public void loginService(final Context context, String username, String password) {
//do some async work, or rest call etc.
//...
//on response, when we want to trigger and send notification that our job is finished
HashMap<String,String> params = new HashMap<String, String>();
params.put("isSuccess", String.valueOf(false));
NotificationCenter.postNotification(context, NotificationType.LoginResponse, params);
}
that's it, cheers!
You could try this: http://developer.android.com/reference/java/util/Observer.html
I found that the usage of EventBus of Guava lib is the simplest way for publish-subscribe-style communication between components without requiring the components to explicitly register with one another
see their sample on https://code.google.com/p/guava-libraries/wiki/EventBusExplained
// Class is typically registered by the container.
class EventBusChangeRecorder {
#Subscribe public void recordCustomerChange(ChangeEvent e) {
recordChange(e.getChange());
}
// somewhere during initialization
eventBus.register(this);
}
// much later
public void changeCustomer() {
eventBus.post(new ChangeEvent("bla bla") );
}
you can add this lib simply on Android Studio by adding a dependency to your build.gradle:
compile 'com.google.guava:guava:17.0'
You could use this: http://developer.android.com/reference/android/content/BroadcastReceiver.html, which gives a similar behavior.
You can register receivers programmatically through Context.registerReceiver(BroadcastReceiver, IntentFilter) and it will capture intents sent through Context.sendBroadcast(Intent).
Note, though, that a receiver will not get notifications if its activity (context) has been paused.
Kotlin: Here's a #Shiki's version in Kotlin with a little bit refactor in a fragment.
Register the observer in Fragment.
Fragment.kt
class MyFragment : Fragment() {
private var mContext: Context? = null
private val mMessageReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
//Do something here after you get the notification
myViewModel.reloadData()
}
}
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
}
override fun onStart() {
super.onStart()
registerSomeUpdate()
}
override fun onDestroy() {
LocalBroadcastManager.getInstance(mContext!!).unregisterReceiver(mMessageReceiver)
super.onDestroy()
}
private fun registerSomeUpdate() {
LocalBroadcastManager.getInstance(mContext!!).registerReceiver(mMessageReceiver, IntentFilter(Constant.NOTIFICATION_SOMETHING_HAPPEN))
}
}
Post notification anywhere. Only you need the context.
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.NOTIFICATION_SOMETHING_HAPPEN))```
PS:
you can add a Constant.kt like me for well organize the notifications.
Constant.kt
object Constant {
const val NOTIFICATION_SOMETHING_HAPPEN = "notification_something_happened_locally"
}
For the context in a fragment, you can use activity (sometimes null) or conext like what I used.
I wrote a wrapper that can do this same job, equivalent to iOS using LiveData
Wrapper:
class ObserverNotify {
private val liveData = MutableLiveData<Nothing>()
fun postNotification() {
GlobalScope.launch {
withContext(Dispatchers.Main) {
liveData.value = liveData.value
}
}
}
fun observeForever(observer: () -> Unit) {
liveData.observeForever { observer() }
}
fun observe(owner: LifecycleOwner, observer: () -> Unit) {
liveData.observe(owner) { observer()}
}
}
class ObserverNotifyWithData<T> {
private val liveData = MutableLiveData<T>()
fun postNotification(data: T) {
GlobalScope.launch {
withContext(Dispatchers.Main) {
liveData.value = data
}
}
}
fun observeForever(observer: (T) -> Unit) {
liveData.observeForever { observer(it) }
}
fun observe(owner: LifecycleOwner, observer: (T) -> Unit) {
liveData.observe(owner) { observer(it) }
}
}
Declaring observer types:
object ObserverCenter {
val moveMusicToBeTheNextOne: ObserverNotifyWithData<Music> by lazy { ObserverNotifyWithData() }
val playNextMusic: ObserverNotify by lazy { ObserverNotify() }
val newFCMTokenDidHandle: ObserverNotifyWithData<String?> by lazy { ObserverNotifyWithData() }
}
In the activity to observe:
ObserverCenter.newFCMTokenDidHandle.observe(this) {
// Do stuff
}
To notify:
ObserverCenter.playNextMusic.postNotification()
ObserverCenter.newFCMTokenDidHandle.postNotification("MyData")
Answer of #Shiki could be right in June 2020, but in January 2022, LocalBroadcastManager happened to be deprecated.
After two days of research, I ended up finding that SharedFlow was indicated by Android to "send ticks to the rest of the app so that all the content refreshes periodically at the same time".
Meaning, more or less, what we could expect from the NSNotificationCenter of Swift.
And here is the way I implemented the Shared Flow in my app:
First, you need to create an InAppNotif Singleton, which is actually a shared ViewModel for your activity (be caution to this last point: shared for your activity, not your all app^^)
enum class InAppNotifName {
NotifNameNumber1,
NotifNameNumber2,
NotifNameNumber3
}
object InAppNotif: ViewModel() {
private val _sharedNotif = MutableSharedFlow<InAppNotifName>(0)
val sharedNotif: SharedFlow<InAppNotifName> = _sharedNotif.asSharedFlow()
private fun sendNotif(name: InAppNotifName) {
CoroutineScope(Default).launch {
_sharedNotif.emit(name)
}
}
public fun notifyNotif1() {
sendNotif(InAppNotifName.NotifNameNumber1)
}
public fun notifyNotif2() {
sendNotif(InAppNotifName.NotifNameNumber1)
}
public fun notifyNotif3() {
sendNotif(InAppNotifName.NotifNameNumber1)
}
}
Second Step, only required if you have many Fragments receiving in app notifications, and you don't want to repeat yourself, would be to create an "Receiving Notif" interface
fun AnyReceivingNotif.observeInAppNotif() {
CoroutineScope(Default).launch {
InAppNotif.sharedNotif.collect {
onReceivingInAppNotif(it)
}
}
}
interface AnyReceivingNotif {
suspend fun onReceivingInAppNotif(value: InAppNotifName)
}
By the way, the "suspend" word is useful only if you need to update the UI upon receiving the notification.
Finally, from any object which is to receive InAppNotif, all you would need to do is get it be conform to your AnyReceivingNotif interface, and then complete the onReceivingInAppNotif function
class MyFragment: Fragment(), AnyReceivingNotif {
override suspend fun onReceivingInAppNotif(value: InAppNotifName) {
when (value) {
InAppNotifName.NotifNameNumber1 -> { /* Do complicated things */ }
InAppNotifName.NotifNameNumber2 -> { /* Do some stuff */ }
InAppNotifName.NotifNameNumber3 -> {
withContext(Default){
/* Update the UI */
}
}
}
}
}
You could use weak references.
This way you could manage the memory yourself and add and remove observers as you please.
When you addObserver add these parameters - cast that context from the activity you are adding it in to the empty interface, add a notification name, and call the method to run interface.
The method to run interface would have a function that is called run to return the data that you are passing something like this
public static interface Themethodtorun {
void run(String notification_name, Object additional_data);
}
Create a observation class that invokes a reference with a empty interface.
Also construct your Themethodtorun interface from the context being passed in the addobserver.
Add the observation to a data structure.
To call it would be the same method however all you need to do is find the specific notification name in the data structure, use the Themethodtorun.run(notification_name, data).
This will send a callback to where ever you created an observer with a specific notification name.
Dont forget to remove them when your done!
This is good reference for weak references.
http://learningviacode.blogspot.co.nz/2014/02/weak-references-in-java.html
I am in the process of uploading this code to github. Keep eyes open!

Categories

Resources