Phonegap plugin for android is not working. Why? - android

I cannot make an Android phonegap plugin work. Not even a single one of the examples I found nor my pathetic failures trying to create one by myself. I first tried with Tutorials like this one. They don't work for me. I always end up with a Cannot call method of undefined error.
So I tried something ready. Got this project from github. It's just a simple plugin to show a toast. I checked everything that i learned on the tutorials:
//the package name in the java
package com.phonegap.toast;
//my class extends Plugin and has a simple show toast method.
public class Tutorial extends Plugin {
#Override
public PluginResult execute(String cmd, JSONArray args, String callback) {
if(cmd.equals("toast"))
{
return showToast(args);
}
return null;
}
private PluginResult showToast(JSONArray args) {
final String message;
try {
message = args.getString(0);
ctx.runOnUiThread(new Runnable()
{
public void run() {
Toast myToast = Toast.makeText(ctx, message, Toast.LENGTH_SHORT);
myToast.show();
}
});
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}
the plugin is defined in res/xml/plugins.xml
plugin name="Tutorial" value="com.phonegap.toast.Tutorial"
and no, if i put it on rex/xml/config.xml it also doesn't work
Last, the method that calls the plugin:
function createToast() {
// i also tried window.Tutorial.showToast('Hello AndroidOpen'); with no success
window.plugins.Tutorial.showToast('Hello AndroidOpen');
}
And here I get the same error again.
10-22 15:39:07.770: E/Web Console(2885): Uncaught TypeError: Cannot call method 'showToast' of undefined at file:///android_asset/www/main.js:123
Any enlightened soul can explain to me what I'm doing wrong? I've been trying this for days, with many different plugins, both my own and even this ones and I can't find out what is it.

Okay, here are a few things that are probably tripping you up. First if the config.xml file exists in res/xml then it will take precedence over plugins.xml. So you will need to add your plugin line to config.xml instead.
Make sure you are including the .js file for your Toast plugin.
Third, window.plugins has been deprecated away so you may need to modify the .js if you are using PhoneGap 2.0.0 or better. Check out my blog post on the topic. The root change is that you now need to new PluginName in your JS as it is no longer put in window.plugins by default.

Related

How do I save app logs locally on Android?

I want to save the logs generated by my application locally on the android device and view them in an instance of a crash.
Using the "Take Bug Report" under the developer options gives the entire system logs which are irrelevant to me. I am looking only for those logs created by my application when it runs.
Is there any application that does this? Or are there any libraries I could include in my application code to satisfy my requirement?
You may just add firebase to your project, and everything will be done automatically.
Or if need it to be "locally", can use the Thread.UncaughtExceptionHandler to save crash log. Register it when your application onCreate.
private static UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;
public static void registerUncaughtExceptionHandler() {
mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
// Save Log
saveLog(ex);
// Throw system
mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
}
});
}
private static void saveLog(Throwable exception) {
try {
String stackTrace = Log.getStackTraceString(exception);
// Save it to SharedPreferences or DB as you like
} catch (Exception e) {
}
}
Then can extract the last crash log, submit to your server or display in logcat when app starts.
It is much better to use Third Party libraries such as Firebase Crashlytics or Sentry Crash Report or AppMetrica for crash reports.
just add these libraries and make an account on one of these sites, then you can have a full report of crashes if happen.
but if you want to save the logs on the device, you can refer to this question :
Saving Logcat to a text file in Android Device
You can try this
fun writeLog(context: Context) {
try {
val path = File(context.filesDir, "log_files")
if (!path.exists()) {
path.mkdir()
}
val fileName = "your_filename.txt"
Runtime.getRuntime().exec("logcat -v time -f $fileName")
} catch (e: IOException) {
}
}
Or you can change logcat command based on your requirements: refer to this https://developer.android.com/studio/command-line/logcat
You can check it at data/data/{applicationId}/files/log_files/

App crashes with "isWhitelistProcess - Process is Whitelisted"

I have two classes TrapCard and EquipmentCard, from which I want to make an object and pass this object through activities. I put a string and a int variable into my parcelable object. The int variable is a reference to an .svg image. And then I'm trying to get the reference from the object to load the .svg image in another activity. For TrapCard this works. But when I'm doing the excat same thing for EquiptmentCard my app crashes with E/Zygote: isWhitelistProcess - Process is Whitelisted
Constructor from EquiptmentCard:
public EquipmentCard(String cardName){
this.cardName = cardName;
switch (cardName){
case "Rope": cardPicRes = R.drawable.ic_rope;
break;
}
}
And here I'm trying to get the reference and load the image
final EquipmentCard equipCard = new EquipmentCard(cardName);
returnIntent.putExtra("card",equipCard);
returnIntent.putExtra("cardType","E");
runOnUiThread(new Runnable() {
#Override
public void run() {
imgViewCard.setImageDrawable(getResources().getDrawable(equipCard.getCardPicRes(), getApplicationContext().getTheme()));
}
});
Here same thing for TrapCard, this works
final TrapCard trapCard = new TrapCard(cardName);
returnIntent.putExtra("card",trapCard);
returnIntent.putExtra("cardType","F");
runOnUiThread(new Runnable() {
#Override
public void run() {
imgViewCard.setImageDrawable(getResources().getDrawable(trapCard.getCardPicRes(), getApplicationContext().getTheme()));
}
});
I'm doing this within the receiveDetections function from the Google mobileVisionAPI
with debugging I can see that my code runs to this line
imgViewCard.setImageDrawable(getResources().getDrawable(equipCard.getCardPicRes(), getApplicationContext().getTheme()));
and then crashes with the mentioned error in logcat.
I put this in try and catch but there is no error I receive. Thank you in advance for your time and have a nice day.
UPDATE1: I set the image in EquipmentCard to ic_spider1 and it worked. I have no clue whats wrong with ic_rope.
UPDATE2: Now i get an exception android.content.res.Resources$NotFoundException: Drawable xxx.xxx:drawable/ic_rope with resource ID #0x7f060072 but i have no clue why. I added this the same way i added spider. Delete and adding again don't work.
UPDATE3: Alright. It seems that my ic_rope.xml has a large string inside. I tried to rebuild my Project and then it shows the message string too large to encode using UTF-8. If i delete the ic_rope.xml i can rebuild without any errors.
Try replacing
imgViewCard.setImageDrawable(getResources().getDrawable(equipCard.getCardPicRes(), getApplicationContext().getTheme()));
with:
setImageResource(R.id.your_image_id)

crashlyticsDidDetectCrashDuringPreviousExecution for crashlytics NDK

We are using crashlyticsDidDetectCrashDuringPreviousExecution to detect java crashes and report them to our BI systems, but our app is mostly C++ and we are using crashlytics NDK, we can't find anything similar to crashlyticsDidDetectCrashDuringPreviousExecution.
Is there any way that we can actually detect an NDK crash when the app starts?
thanks
Oded
Mike from Fabric here.
Currently, there isn't a way to do this within Fabric or the SDK for an NDK crash.
NOTE: This works on older version only (Crashlytics 2.6.7 and CrashlyticsNDK 1.1.6)
I'm also looking for a solution for this.
We currently found a partial solution. I'm not sure how good it is, it's definitely not official, plus it's asynchronic (which we're trying to overcome by looping), but it's the best solution I found and it seems like it's working
Fabric.with(this, new Crashlytics.Builder().core(core.build()).build(), new CrashlyticsNdk(), new Crashlytics());
if (!userLeft) { // our handling to fix bug, see explanation below
new Thread(new Runnable() {
#Override
public void run() {
SessionEventData crashEventData = null;
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000); // in ms
} catch (InterruptedException e) { }
crashEventData = CrashlyticsNdk.getInstance().getCrashEventData();
if (crashEventData != null)
{
// there was a crash!
// crash timestamp can be found at crashEventData.timestamp
break;
}
}
}
}).start();
}
Explaination for userLeft:
We had some bug with reporting crash for users that exited app, and this is the solution for that. We set this flag to true, and save it on the device (SharedPreferences). We do it on our main activity (which extends NativeActivity), on finish() func.
Code:
#Override
public void finish() {
// set some key such as USER_LEFT to TRUE
super.finish();
}
After that, just get that USER_LEFT value, assign it into userLeft param, and set it back to false on SharedPerferences.
Any insights about this solution?

Error integrating Aurasma: Resource Integrity check failed

I am trying to Integrating Aurasma in my application. All application work well but on Aurasma part when I launch it on Button Click IT throws a message on splash screen as "An error is occurred" and on Log Cat It shows "Resource integrity check failed" I am wondering why this is happening, I integrate aurasma on a separate application without any click event, it launches directly then it works but in side of my application its not working, why. I am sure about these points:
Make sure the SDK tools are version 14 or above.
Check the Eclipse project to make sure that AurasmaKernel is set as required on the build path
Check that the AurasmaKernel package has built properly in Eclipse (also try building it manually)
Make sure that the kernel is correctly extracted, and that your resources don't clash with any of the packaged library
But yet it not working same error message.
Code for launching Aurasma is below:
aurasmaIntent = AurasmaIntentFactory.getAurasmaLaunchIntent(HomeActivity.this,
getString(R.string.app_name), getString(R.string.app_version));
} catch (AurasmaLaunchException e) {
Log.e("AKTest", "Error getting intent", e);
showDialog(DIALOG_ERROR);
return;
}
if (DELAY_START) {
AurasmaSetupCallback callback = new AurasmaSetupCallback() {
#Override
public void onLoaded() {
dismissDialog(DIALOG_PROGRESS);
startActivity(aurasmaIntent);
}
#Override
public void onLoadWarning(final int code) {
Log.w("AKTest", "Preload warning: " + code);
}
#Override
public void onLoadFail(final int code) {
Log.e("AKTest", "Preload error: " + code);
dismissDialog(DIALOG_PROGRESS);
showDialog(DIALOG_ERROR);
}
};
showDialog(DIALOG_PROGRESS);
AurasmaIntentFactory.startAurasmaPreload(getApplicationContext(), aurasmaIntent,
callback);
} else {
startActivity(aurasmaIntent);
}
}
If you change some resource from Aurasma library(layout or string) you will get this error - "An error is occurred". Library checks resources on Aurasma start. Don't change or delete any files.
Another thing that can cause error is:
aurasmaIntent = AurasmaIntentFactory.getAurasmaLaunchIntent(HomeActivity.this,
getString(R.string.app_name), getString(R.string.app_version));
Here second parameter is userAgentName. This is the name of your app that you have from studio.aurasma.com. In "Make your own app" you can see the application name - this name is connected with your application but can be different.
check your minSdkVersion in the manifest
android:minSdkVersion="8"

android usage of "controller.query(activity);" in scoreloop

i am attempting to implement a built in controller that is part of the scoreloop library. the documentation states:
Basic Usage:
To invoke the TOS dialog if it was not accepted previously, the following code may be used:
final TermsOfServiceController controller = new TermsOfServiceController(new TermsOfServiceControllerObserver() {
#Override
public void termsOfServiceControllerDidFinish(final TermsOfServiceController controller, final Boolean accepted) {
if(accepted != null) {
// we have conclusive result.
if(accepted) {
// user did accept
}
else {
// user did reject
}
}
}
});
controller.query(activity);
but when i paste this into my code i get the following syntax errors:
am i using this incorrectly? how and where would this be used any ideas?
EDIT: after moving the statement to the method where i want to show the dialog i now get the following error:
You seem to be calling controller.query(activity) in a class body where a declaration is expected. Move the statement controller.query(activity) to a method where you would like to show the dialog.

Categories

Resources