I'm trying to integrate DynamoDB into a mobile app I'm developing using Android Studio and Kotlin. I've been following the AWS instructions for Android SDK 2.0 outlined here: https://aws-amplify.github.io/docs/sdk/android/start
The instructions are in Java, which I'm not very familiar with. I've been able to get things to work until step 5, which involves integrating the AWS AppSync client into the onCreate() method in my application. I tried to copy and paste the code into Android Studio, which then tries to convert the Java code to Kotlin, but I'm getting a lot of errors. Are there any Java/Kotlin experts out there that might be able to convert the following code to Kotlin for me?
private AWSAppSyncClient mAWSAppSyncClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAWSAppSyncClient = AWSAppSyncClient.builder()
.context(getApplicationContext())
.awsConfiguration(new AWSConfiguration(getApplicationContext()))
.build();
}
And
public void runMutation(){
CreateTodoInput createTodoInput = CreateTodoInput.builder().
name("Use AppSync").
description("Realtime and Offline").
build();
mAWSAppSyncClient.mutate(CreateTodoMutation.builder().input(createTodoInput).build())
.enqueue(mutationCallback);
}
private GraphQLCall.Callback<CreateTodoMutation.Data> mutationCallback = new GraphQLCall.Callback<CreateTodoMutation.Data>() {
#Override
public void onResponse(#Nonnull Response<CreateTodoMutation.Data> response) {
Log.i("Results", "Added Todo");
}
#Override
public void onFailure(#Nonnull ApolloException e) {
Log.e("Error", e.toString());
}
};
I haven't found many resources on integrating DynamoDB in an AndroidStudio Kotlin project, so if there are better ways of doing this, I'd love to hear them.
Related
All the examples of invoking the Android camera with androidx.camera.core.ImageCapture are in Kotlin or are really out of date, and I am getting weird compilation errors with Java.
androidx.camera.core.ImageCapture imageCapture;
...
protected void onCreate(Bundle savedInstanceState) {
...
imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
...
}
public void snapQRCode(View view) {
imageCapture.takePicture(executor, new androidx.camera.core.ImageCapture.OnImageCapturedCallback(){
void onCaptureSuccess(ImageProxy image){}
void onError(ImageCaptureException exception) {}
}
);
}
This throws a compile error:
error: onCaptureSuccess(ImageProxy) in anonymous
xyz.MainActivity$1 cannot override
onCaptureSuccess(ImageProxy) in OnImageCapturedCallback
void onCaptureSuccess(ImageProxy image){}
I am confused because ImageCapture.OnImageCapturedCallback is abstract. I shouldn't be overriding at all. I am pretty sure I have the camera configured correctly, but if someone could explain this error, and perhaps provide a few lines of (Java) code that snaps the picture and returns the image, that would be much appreciated.
Here are the libraries I am using in app/build.gradle:
def cameraxVersion = "1.0.0-beta03"
implementation "androidx.camera:camera-core:${cameraxVersion}"
implementation "androidx.camera:camera-camera2:${cameraxVersion}"
implementation "androidx.camera:camera-lifecycle:${cameraxVersion}"
MainActivity cannot be converted to LifecycleOwner
I used this as LiveCycle Owner, but it is rejected and I got an error as you see in the picture.
I work on Api 25 and I this the problem may be related to this version
This is info about my sdk
compileSdkVersion 25
buildToolsVersion '25.0.2'
This is my code:
private void retrieveTasks() {
Log.d(TAG, "Actively retrieving the tasks from the DataBase");
// Extract all this logic outside the Executor and remove the Executor
// Fix compile issue by wrapping the return type with LiveData
LiveData<List<TaskEntry>> tasks = mDb.taskDao().loadAllTasks();
// Observe tasks and move the logic from runOnUiThread to onChanged
tasks.observe(this, new Observer<List<TaskEntry>>() {
#Override
public void onChanged(#Nullable List<TaskEntry> taskEntries) {
Log.d(TAG, "Receiving database update from LiveData");
mAdapter.setTasks(taskEntries);
}
});
}
I put LiveData dependencies in my Gradle
compile "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
If anyone knows the reason for the problem, let me know please
Fragments and Activities in Support Library 26.1.0 and later already implement the LifecycleOwner interface by default
but in version 25 you need to implement LifecycleOwner interface
for example
public class MyActivity extends Activity implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
#Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(Lifecycle.State.STARTED);
}
#NonNull
#Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
Source : Handling lifecycles with lifecycle-aware components
As you can read here the LifecycleOwner was added in support library 26.1.0. Easiest way to fix your issue would be upgrading your support library version.
Had the same error. Upgrading to androidx support libraries fixed the issue.
Choose inside Android Studio: Refactor -> Migrate to android x
thanks for read my question. (pleaase apologize my bad english)
I'm trying to develop a simple android prototype using socket.io.
i've got a server node.js with socket.io (is working), and now i'm develop the client side (there is the problem).
I've download the zip on :
https://github.com/nkzawa/socket.io-client.java
On Eclipse, i have do a right click on my projet/ Build Path / Add external Archive
and i selected the downloaded file.
The client code :
package com.example.temp_test;
import java.net.URISyntaxException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Socket socket;
try {
socket = IO.socket("X.X.X.X:8080");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
#Override
public void call(Object... args) {
socket.emit("message", "hi");
socket.disconnect();
}
}).on("event", new Emitter.Listener() {
#Override
public void call(Object... args) {}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
#Override
public void call(Object... args) {}
});
socket.connect();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Of course X.X.X.X is an IP adress.
So now i've got some red lines because eclipse doesn't find the IO class.
I'm already tried tutorials, like this : Java implementation for client Socket.io compatible with version 1.0 (i've got some crash), many other are too old or not compatible 1.0.
I'm juste a beginner with on socket.io android.
So if someone can help me, i just want the way to install good packages without crash, that will be very appreciate and i will can do the rest myself.
Thanks in advance
I am also going through the same issue. I tried gottox but we also have v1.0 on nodejs server so it didn't work out. I just found that https://github.com/koush/ion is a more simple and stable approach to implement socketio client on Android. I'll try this tomorrow and maybe you can also try and we can share our findings. This offers jar file so at least you don't have to go through the maven path.
I just saw your post, it's an old post, but probably my answer could help to other beginners like me.
The socket.io.client needs the engine.io-client library. The url of the source code is here: https://github.com/nkzawa/engine.io-client.java
Does someone knows if it is possible to add push notifications(like Amazon Simple Notification Service) in an Android and iOS with RoboVM libGDX projects? And if it is possible, are there any good tutorials or good hints how to implement such things?
I would be happy about every hint how I can implement it.
Hi I know this is an old question but I was struggling to find a solution for this specially for iOS, but I finally found a way. If the explanation below is confusing and you prefer to see an example here is a github repo with a sample project:
Repo GitHub
I only show the code for iOS see the repo for Android.
The idea is simple you need to create a class that handles sending a notification for each platform on each of your projects (Android and iOS) and have it implement an interface called NotificationsHandler.
NotificationsHandler:
public interface NotificationsHandler {
public void showNotification(String title, String text);
}
iOS Adapter:
public class AdapteriOS implements NotificationsHandler {
public AdapteriOS () {
//Registers notifications, it will ask user if ok to receive notifications from this app, if user selects no then no notifications will be received
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Alert, null));
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Sound, null));
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Badge, null));
//Removes notifications indicator in app icon, you can do this in a different way
UIApplication.getSharedApplication().setApplicationIconBadgeNumber(0);
UIApplication.getSharedApplication().cancelAllLocalNotifications();
}
#Override
public void showNotification(final String title, final String text) {
NSOperationQueue.getMainQueue().addOperation(new Runnable() {
#Override
public void run() {
NSDate date = new NSDate();
//5 seconds from now
NSDate secondsMore = date.newDateByAddingTimeInterval(5);
UILocalNotification localNotification = new UILocalNotification();
localNotification.setFireDate(secondsMore);
localNotification.setAlertBody(title);
localNotification.setAlertAction(text);
localNotification.setTimeZone(NSTimeZone.getDefaultTimeZone());
localNotification.setApplicationIconBadgeNumber(UIApplication.getSharedApplication().getApplicationIconBadgeNumber() + 1);
UIApplication.getSharedApplication().scheduleLocalNotification(localNotification);
}
});
}
}
Now by default Libgdx passes your ApplicationListener or Game object to AndroidLauncher and IOSLauncher along with a configuration object. The trick is to pass the class we created earlier to the ApplicationListener so that you can use it inside your Core project. Simple enough:
public class IOSLauncher extends IOSApplication.Delegate {
#Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
// This is your ApplicationListener or Game class
// it will be called differently depending on what you
// set up when you created the libgdx project
MainGame game = new MainGame();
// We instantiate the iOS Adapter
AdapteriOS adapter = new AdapteriOS();
// We set the handler, you must create this method in your class
game.setNotificationHandler(adapter);
return new IOSApplication(game, config);
}
public static void main(String[] argv) {
NSAutoreleasePool pool = new NSAutoreleasePool();
UIApplication.main(argv, null, IOSLauncher.class);
pool.close();
}
}
Now that you have a reference to the implementation of NotificationHandler you can simply call it through your Core project.
public class MainGame extends Game {
// This is the notificatino handler
public NotificationHandler notificationHandler;
#Override
public void create () {
// Do whatever you do when your game is created
// ...
}
#Override
public void render () {
super.render();
// This is just an example but you
// can now send notifications in your project
if(condition)
notificationHandler.showNotification("Title", "Content");
}
#Override
public void dispose() {
super.dispose();
}
// This is the method we created to set the notifications handler
public void setNotificationHandler(NotificationHandler handler) {
this.notificationHandler = handler;
}
}
One last thing
If you need to run the Desktop version then you will need to do the same thing for Desktop otherwise you might get errors, it will not do anything on the Desktop, or you can check the platform before calling the method showNotfication. You can clone the repo where I do this:
Repo GitHub
I've never done it myself. But you can use this tutorial to find out how to write Android specific code in your libGDX project. Your Android code could then receive the notifications and trigger a callback in libGDX. I hope this is at least a step in the right direction.
However I' not sure about doing the same for iOS.
I am trying to integrate facebook to my Android application. When i added internet permission in the project and have generated App_ID but when I put facebook files in to my project it gives these errors as shown in pic.
Now should I remove #Override or am I missing something.
If I put this code into my OnCreate method it also gives errors to remove #Override.
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
Should I include facebook.apk if yes then Where to add this? please help
I followed many tutorials but could not resolve these errors.
This is most likely because you are switching between Java 1.5 and Java 1.6. In 1.5 you couldn't mark interface implementations with #Override, but you can in 1.6.
#Override annotation error (android prefs)
Bug with Override annotations in Eclipse