I'm using Azure Face API for facial recognition, some of my methods are only running correctly in debug mode:
public void detectFace(MenuItem item){
getPicture();
startFaceRecognition();
}
I first noticed the issue with the code above. The getPicture() method does not seem to run if the second method is called after, though does run if the startFaceRecognition() is called from within getPicture().
public void addFace(MenuItem item){
getPicture();
AddFaceParams params = new AddFaceParams("family", "personIdCode", "", currentPhotoPath);
new AddFaceTask().execute(params);
}
The second piece of code takes a picture and then sends the picture with relevant information to Azure with a HTTP request in an asynchronous task. I believe the issue is the getPicture() method isn't being called unless there's a breakpoint placed inside the method. It works if I step through the method, but not if it is run by itself.
Would this be some form of timing issue?
I am new to Java and Android development.
Related
I found this bug on with the following code:
(it.context.get() as? Activity)?.runOnUiThread(Runnable {
it.weakRefIOnDataRefreshed.get()?.onDataRefreshed(refreshedItemList)
})
The code above is inside a run method that runs in a non UI Thread. I want to call a method in fragment with the refreshedItemList as argument. I want to debug the onDataRefreshed method (which is inside a Fragment), and I'm putting inside this function a break point but nothing happens. I also put a Log method to make sure that the code is running and the Log method prints indeed. What may be the problem that the debugger doesn't stop on the line which I have marked?
In your class where that background thread works define a Handler object and use post method to update UI e.g. (Java).
// Define handler object
Handler handler = new Handler(Looper.getMainLooper);
...
...
// Here ise where you want to update your UI
handler.post(() -> {
// Your code with which you want to update the UI
}
I am trying to implement a feature of my Flutter application where the user will be reminded about something every 15 minutes through a notification banner. Since the interval of this reminder is pretty long, I intend to have a background service run in the background that would interface through some kind of an alarm manager that will execute the code to show the notification intermittently. Also, note that the interval of the reminders doesn't need to be exact.
My implementation plan is to use two Flutter packages in unison (flutter_local_notificationand android_alarm_manager). I cannot use flutter_local_notifications alone because the interval of a repeating notification is limited to one minute, one hour, one day, and so on whereas my app must have an interval of 15 minutes. With that, I completely removed the scheduling function from the flutter_local_notification package and gave that responsibility to android_alarm_manager.
I used these two of this packages by creating separate classes for each of them. The first class for the flutter_local_notifications package has something similar to a class from this article: https://itnext.io/local-notifications-in-flutter-6136235e1b51. On the other hand the class for android_alarm_manager has the following implementation in notification_alarm_manager.dart:
class NotificationAlarmManager {
NotificationAlarmManager() {
AndroidAlarmManager.initialize();
}
//I used interval in seconds here only for testing
void startNotifications(int intervalInSeconds) async {
await AndroidAlarmManager.periodic(
Duration(seconds: intervalInSeconds),
0,
_showRepeatingNotification,
);
print('Periodic alarm initialized');
}
static void _showRepeatingNotification() async {
print('Repeating notification will be shown');
var notification = LocalNotificationsPlugin();
await notification.showSingleNotification();
}
}
This class is instantiated from the main function with this implementation:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var alarmManager = NotificationAlarmManager();
runApp(CushionSensorApp(alarmManager));
}
The notifications are triggered to start from an the initState() function of the CushionSensorApp state as follows:
class _CushionSensorAppState extends State<CushionSensorApp> {
#override
void initState() {
...
this.widget.alarmManager.startNotifications(15);
}
...
}
The application builds correctly but whenever it's time for the android_alarm_manager to execute _showRepeatingNotification(), the primary error and a bunch of others will repeatedly be displayed instead:
MissingPluginException(No implementation found for method initialize on channel dexterous.com/flutter/local_notifications)
...
Yes, I already followed all the initial requirements for the android_alarm_manager (adding permissions, receivers). Also I had already added the custom Application.java file as an override and changing android:name to .Application. I even tried using the Kotlin file from this Github issue https://github.com/MaikuB/flutter_local_notifications/issues/238 but it still does not work.
Please refer to my repository for my details about my code (https://github.com/lewisbolaton/cushion_sensor_app).
I know this is an error with accessing memory outside the readspace but i have absolutely no idea how to fix this. I'm new to android, so i don't exactly know how to print out a more detailed error list from logcat in eclipse. I've tried everything from disposing literally everything, to calling System.gc to setting all my variables to null. However, whenever i switch screens the fatal signal occurs. I just need someone to tell me what exactly is going on or how i could get more details about the error.
I had the same error, what solved it was to make sure i'm on the UI thread, like this:
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
// Your crashing code here
}
});
In my case i received same error when i try to create a new body and attach it's fixture, from beginContact (inside Contact Listener). After i moved outside Contact Listener my body creation everything was ok. Probably some conflict appears in Fixture createFixture (FixtureDef def) because according to manual: Contacts are not created until the next time step.
So, I want to display an image in an ImageView that is downloaded from a website and saved on the device's file system. The downloading of the image is done by calling a method in another class (in one of my separate library projects). This method, in turn, calls an AsyncTask to do the downloading in another thread. On the post execute of this AsyncTask, I want to set the image for the ImageView. Keep in mind that this separate library is independent of my main app and knows nothing about my UI (and I wish to keep it this way).
I have a method in the UI portion of my code (in a fragment) that sets the image for this ImageView. I'm thinking it would be nice to pass this method to the library method that retrieves the image. That way, when the download is complete, onPostExecute() could call this UI method that I have. The dilemma I'm facing is I don't want any compile-time dependencies for my library that references my UI/ImageView.
Finally, not every time when this AsyncTask is called do I want to do anything in the post execute. In other words, any post execute logic is completely optional.
General Flow:
1) Code in my fragment calls MyLib.saveRemoteFile(url, destinationFilename)
2) MyLib.saveRemoteFile() downloads the image and saves it to a file by way of a thread (via an AsyncTask)
3) As soon as the image is downloaded and saved, I want to set the ImageView.setImageBitmap() using the bitmap from the saved file
I've looked at the Command pattern, but am stuck on how to implement it in this particular scenario. Any tips on this?
Maybe there is another approach that can be used? (without MyLib.saveRemoteFile() having compile-time dependencies to the UI)?
UI Fragment code snippet:
.
boolean wasSaved = MyLib.saveRemoteFile(url, destinationFilename, true)
.
.
.
Library code snippet (in a different library project):
public static boolean saveRemoteFile(String url, String filename, boolean overwriteExistingFile)
{
.
// code to check if directory exist and creates it if needed, etc, etc...
.
.
.
new AsyncTask<String, Void, Boolean>() {
#Override
protected Boolean doInBackground(String... params)
{
// code to download and save image file...
.
.
.
}
#Override
protected void onPostExecute(Boolean result)
{
// *** this is where I want to take the ImageView and set its bitmap image.
}
}.execute(new String[] {url, filename});
return retVal;
}
Design is flawed, library should have provided you Observer design pattern, using which you would have submitted listener reference along with downloading details.
Upon finishing downloading, library using your provided listener would have notified back to you over download process result, reading which you could have taken action easily with UI.
Moreover in your library code saveRemoteFile() spawns a asynctask, return value is irrelevant here as method wont wait until asynctask is executed.
Possible implementation as per above mentioned info.
// Interface for bserving
public interface DownloadListsner{
public void onSuccess(String uri);
public void onError(String uri);
}
// API for submitting download request
public void saveRemoteFile(String url, String filename,DownloadListsner listener... ){
...
}
AsyncTask{
// Upon finish execution
onPostExecute(){
// Take action
listener.onSuccess
or
listener.onError
}
}
Command pattern
You should be rather implementing Observer pattern and your download library should allow other code to attach listeners and be notified about when certain action is completed/started/whatever. And since you are just being informed that something happened you can have your code that reacts on that notification completely different in every part of your app if you need so.
I am making a library where an application can use it to capture a selection of a screen and convert it to an image, like Gyazo.
This library is not the application itself, but only the tool that returns the File or BufferedImage object to the application.
I want the application to be simple as this:
Bootstrap b = new Boostrap(new GifCapturer());
b.beginCapture(); // user selects an area
File file = b.getFile();
But how can I make the application wait till the library returns the object? as you see the beginCapture method should activate the JFrame where the user will select an area to capture.
Do I need to sleep the thread? or use listeners design?
The beginCapture method starts a jframe window, where the user is able to select an area of the screen. Once selected, the library will convert the selected area to an object and set it as a local variable. So when you will use getFile it ill return the captured image. But the thing is, i need to make sure that the image was selected before getFile call gets executed, and wait instead but im not sure how.
Sorry if the question is not detailed, im on phone.
Please let me know if you need more information.
Implement a listener, that is invoked as soon the selection is ready. Put your File file = b.getFile(); code into the listener.
The code of your JFrame would be necessary to give a more detailed answer.
I have decided to use a Listener with a own built listener class, and interface.
Create an interface which you will use to get the data, or that will get know when the listener gets called, like this in my case:
public static void main(String[] args) throws AWTException {
Bootstrap b = new Bootstrap(new GifCapturer());
b.beginCapture(new ScreenCaptureCallback() {
#Override
public void captureEnded(File file) {
System.out.println("done!");
}
});
}