Android create Toast using class reflection? - java

I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.

Related

How can I get extra data by Intent using Android annotation?

I'm really new to Android. I'm trying to pass the data to the text view of another class. But for some reason, it does not appear in the second class / page (the textview didnt change). I was able to do it when I was not using android annotation, but I was told that using annotations is a lot easier so I'm trying to convert it to that but for some reason it isn't working? I might be missing something.
Main 1
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.world)
public class MainActivity extends AppCompatActivity {
#ViewById(R.id.userName)
EditText user;
#Click(R.id.signIn)
public void signInButton(){
Main2Activity_.intent().name(user.getText().toString()).start();
}
Main 2
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.welcome)
public class Main2Activity extends AppCompatActivity {
#Extra("name")
String n;
#ViewById(R.id.name)
TextView tv;
Intent in2 = getIntent();
#AfterViews
void home(){
tv.setText("Hello, " + n + " !");
}
}
In your receiving Activity (for example MyActivity) you must declare the name of the object you want to receive like that
#Extra("name")
String name;
To pass data, when you create your intent you must do something like this:
Main2Activity_.intent(this).name(user.getText().toString()).start();
If you need a more precise solution edit your question to show more of your code.
You can look at the official doc for a more complete example
It might have to do with the dependencies you should add for the annotations to your project_root/build.gradle and app/build.gradle files.
Make sure to check this thread: what is Android Annotations and why we use Android Annotations?

Unable to import a Scala object into the Java project

I created a Scala object:
package myapp.data
import java.io.File
import myapp.models.NodeViewModel
import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.io.xml.DomDriver
object ForumSerializer {
def openFile(file : File) : NodeViewModel = {
// doing something
}
def saveToFile(model : NodeViewModel) : Unit = {
// doing something
}
}
Then I tried to import it in another Java file
import myapp.ForumSerializer;
The error I get is:
Import myapp.ForumSerializer cannot be resolved.
What am I doing wrong?
Import it as ForumSerializer$.
Scala adds a $, so the compiler doesn't get confused with the class, when you have both an object and a class of the same name. You can then access the singleton object using the generated MODULE$.

How to create/use/import a custom made Method on Java

Like the in-built Math class, there are a couple of methods that one can use without importing the Math class. e.g
int io = (int) Math.random();
and notice the import region: no MATH whatsoever
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
but seeing, Math set doesn't have everything i needed, i created mine in a new class, but i can't seem to figure out what to do so i can be able to use it.
Taking a hint from the Math.java file, I've made my class final and my methods static but no avail..
Here's an excerpt of my code
package customops.Sets;
/**
*
* #author Kbluue
*/
public final class SetOpz {
public SetOpz(){}
public static int setMax(int[] set){
int out = set[0];
for(int i=1; i<set.length; i++){
out = Math.max(out, set[i]);
}
return out;
}
how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?
You don't need to import Math explicitly because it is included by default. To use your own code you will have to import it. If you're using IntelliJ or Eclipse or some other smart IDE it will offer to import it for you automatically. Otherwise add a import statement at the top:
import customops.Sets.SetOpz;
You can import your method wherever you want to use with the following import statement
import static customops.Sets.SetOpz.setMax;

The method is undefined for the type in Eclipse

Method is undefined for the type in Eclipse. Can't seem to solve it. The error is in the lines: msg.setTimestamp( System.currentTimeMillis() ); and msg.setBody("this is a test SMS message");
package com.example.smsnotification;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
public class PopSMSActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Retrieve serializable sms message object by the key "msg" used to pass to it
Intent in = this.getIntent();
PopMessage msg = (PopMessage) in.getSerializableExtra("msg");
//Case where we launch the app to test the UI EG: No incoming SMS
if(msg==null){
msg = new PopMessage();
con.setPhone("0123456789");
msg.setTimestamp( System.currentTimeMillis() );
msg.setBody("this is a test SMS message");
}
showDialog(msg);
}
Remove the code and write it back to eclipse. It worked for me....You can try copy and paste to after writing signature of function/class.
This means the PopMessage class doesn't provide the methods setTimestamp(long) and setBody(String).
There is no import statement for PopMessage in your code so I assume it is a class which you have implemented and is contained in the same package as the Activity which you have posted.
So you could either solve this by implementing those two methods in PopMessage or by removing the calls.
You may also extend your Eclipse Settings by activating the "save Actions" (Window->Preferences->Java->Editor->Save Actions) and use the Option "Organize Imports". This would at least add the propbably missing Import "...PopMessage" while you press Ctrl+S.

Native functions throw UnsatisfiedLinkError in custom view, despite working in main activity

For some reason I can only call native functions from my main activity and not any custom views that I've created. Here is an example file (I followed a tutorial, but renamed the classes http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/)
See the usage of the native function "getNewString".
package com.example.native;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
public class NativeTestActivity extends Activity
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(new BitmapView(this));
String hello = getNewString(); // This line works fine
new AlertDialog.Builder(this).setMessage(hello).show();
}
}
class BitmapView extends View
{
static
{
System.loadLibrary("nativeTest");
}
private native String getNewString();
public BitmapView(Context context)
{
super(context);
String hello = getNewString(); // This line throws the UnsatisfiedLinkError
new AlertDialog.Builder(this.getContext()).setMessage(hello).show();
}
}
How can I call native functions in my custom views?
I've built the application as an Android 2.2 app. I'm running the application on my HTC Desire. I have the latest SDK (9) and latest NDK (r5).
Your problem is that you are trying to call the native function from a class where it dont belongs to.
You defined the following JNI function in your c file:
jstring Java_com_example_native_NativeTestActivity_getNewString()
This states that the native function when loaded will bind with the method declared as native in NativeTestActivity class. So when you try to call it from your View class it doesn't find any function to bind to.
In that case it will look for the following function (which of course does not exist in your .so):
jstring Java_com_example_native_BitmapView_getNewString()
If you still want to be able to call the same function from different classes you can declare it in a container class that can be accessed from any class you want.
eg:
java code:
package com.example.native;
public class NativeHelper {
public native String getNewString();
static
{
System.loadLibrary("nativeTest");
}
}
c code:
jstring Java_com_example_native_NativeHelper_getNewString(JNIEnv* env, jobject javaThis)
{
return (*env)->NewStringUTF(env, "Hello from native code!");
}

Categories