Android Question the third party sdk

bjfhs

Active Member
Licensed User
This is my first time to use B4A(My English is poor),I want use third party sdk.
java code
public static void setDebugMode(boolean debugEnalbed)
public static void init(Context context)
public static void setAlias(Context context, String alias, TagAliasCallback callback)
B4X:
private void setAlias() {EditText aliasEdit = (EditText) findViewById(R.id.et_alias);String alias = aliasEdit.getText().toString().trim();if (TextUtils.isEmpty(alias)) {
 Toast.makeText(PushSetActivity.this,R.string.error_alias_empty, Toast.LENGTH_SHORT).show();return;}
 if (!ExampleUtil.isValidTagAndAlias(alias)) {
 Toast.makeText(PushSetActivity.this,R.string.error_tag_gs_empty, Toast.LENGTH_SHORT).show();
 return;
 }

 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));
}

private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
 @Override
 public void gotResult(int code, String alias, Set<String> tags) {
 String logs ;
 switch (code) {
 case 0:
 logs = "Set tag and alias success";
 Log.i(TAG, logs);
 break;
 case 6002:
 logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
 Log.i(TAG, logs);
 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
 break;
 default:
 logs = "Failed with errorCode = " + code;
 Log.e(TAG, logs);
 }
 ExampleUtil.showToast(logs, getApplicationContext());
 }
};
private static final int MSG_SET_ALIAS = 1001;
private final Handler mHandler = new Handler() {
@Override
 public void handleMessage(android.os.Message msg) {
 super.handleMessage(msg);
 switch (msg.what) {
 case MSG_SET_ALIAS:
 Log.d(TAG, "Set alias in handler.");
 
 JPushInterface.setAliasAndTags(getApplicationContext(),
 (String) msg.obj,
 null,
 mAliasCallback);
 break;
 default:
 Log.i(TAG, "Unhandled msg - " + msg.what);
 }
 } 
};

My B4A code
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("cover")
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface").RunMethod("init",Array(GetContext))
    jpMsg.RunMethod("setDebugMode",Array (True))
    
''''''''''next 3 line is wrong,how to do it
    Dim jvo As JavaObject
    Dim Callback As Object = jvo.CreateEvent("cn.jpush.android.api.TagAliasCallback", "Callback", Null)
    jpMsg.RunMethodjo("setAlias",Array(GetContext,"ceshi",Object(jvo))
End Sub

Sub  Callback(responseCode As Int , alias As String, tags As String)
   
End Sub

Sub GetContext As JavaObject
    Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetFieldJO("processBA")
End Sub
 

bjfhs

Active Member
Licensed User
Thank you for your reply.
I try it one day,but it can't work.
B4X:
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface").RunMethod("init",Array(GetContext))
    jpMsg.RunMethod("setDebugMode",Array (True))

    Dim jvo As JavaObject
    'jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback") 
  
    jvo.InitializeNewInstance("cn.jpush.android.api.TagAliasCallback", Null)
     Dim ctxt As JavaObject
    ctxt.InitializeContext
    jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",jvo))
jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",jvo)) can't work
InitializeStatic and InitializeNewInstance is test.
 
Upvote 0

bjfhs

Active Member
Licensed User
when I compile it ,nothing in log.
the Emulator stopped without anything in the log
So i don't know how to do?
 
Upvote 0

bjfhs

Active Member
Licensed User
Yes,I 'm use B4A-bridge,and release mode. Today I use debug mode with Emulator.The logs are show.
LogCat connected to: emulator-5554
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
** Service (starter) Create **
** Service (starter) Start **
Copying updated assets files (1)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 46 (Main)
java.lang.InstantiationException: can't instantiate class cn.jpush.android.api.TagAliasCallback
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:85)
at bjfhs.guo.com.main._activity_create(main.java:399)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at bjfhs.guo.com.main.afterFirstLayout(main.java:102)
at bjfhs.guo.com.main.access$000(main.java:17)
at bjfhs.guo.com.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Upvote 0

bjfhs

Active Member
Licensed User
when I use the next code
B4X:
jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback")
the log:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 51 (Main)
java.lang.RuntimeException: Method: setAlias not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
    at anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:138)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at bjfhs.guo.com.main.afterFirstLayout(main.java:102)
    at bjfhs.guo.com.main.access$000(main.java:17)
    at bjfhs.guo.com.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
when I use the next code
B4X:
jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback")
the log:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 51 (Main)
java.lang.RuntimeException: Method: setAlias not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
    at anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:138)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at bjfhs.guo.com.main.afterFirstLayout(main.java:102)
    at bjfhs.guo.com.main.access$000(main.java:17)
    at bjfhs.guo.com.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

Strain that error you have in this line, because i see in your log:
java.lang.RuntimeException: Method: setAlias not matched.
So maybe you call this method after line
B4X:
jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback")
If so, you check what you put as parameter in method setAlias, for sure you have one thing wrong
 
Upvote 0

bjfhs

Active Member
Licensed User
Thank MarcoRome
you can check my code
B4X:
    Dim ctxt1 As JavaObject
    ctxt1.InitializeContext
  
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface").RunMethod("init",Array(ctxt1))
    jpMsg.RunMethod("setDebugMode",Array (True))

    Dim jvo As JavaObject
    jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback")
  
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",jvo))
java code
B4X:
java code
public static void setDebugMode(boolean debugEnalbed)
public static void init(Context context)
public static void setAlias(Context context, String alias, TagAliasCallback callback)
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Thank MarcoRome
you can check my code
B4X:
    Dim ctxt1 As JavaObject
    ctxt1.InitializeContext
 
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface").RunMethod("init",Array(ctxt1))
    jpMsg.RunMethod("setDebugMode",Array (True))

    Dim jvo As JavaObject
    jvo.InitializeStatic ("cn.jpush.android.api.TagAliasCallback")
 
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",jvo))
java code
B4X:
java code
public static void setDebugMode(boolean debugEnalbed)
public static void init(Context context)
public static void setAlias(Context context, String alias, TagAliasCallback callback)

When you post a problem, pls. Give more details possible. Because so isnt possible help you.
This TagAliasCallback what come back "Event" ??
if yes you can try this:
B4X:
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    Dim callback as JavaObject
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface")
    callback = Jo.CreateEventFromUI("cn.jpush.android.api.TagAliasCallback", "callback", Null)
   
   
    jpMsg.RunMethod("init",Array(ctxt))
    jpMsg.RunMethod("setDebugMode",Array (True))

    jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",callback))
   
   
   
   
Sub Callback_Event (MethodName As String, Args() As Object)
    Log("Method: " & MethodName)
    Log(Args(0))
End Sub
 
Upvote 0

bjfhs

Active Member
Licensed User
B4X:
Dim ctxt1 As JavaObject
    ctxt1.InitializeContext
   
    jpMsg.InitializeStatic("cn.jpush.android.api.JPushInterface").RunMethod("init",Array(ctxt1))
    jpMsg.RunMethod("setDebugMode",Array (True))
    Dim jvo As JavaObject
    Dim Callb As Object = jvo.CreateEvent("cn.jpush.android.api.TagAliasCallback", "Callback", Null)
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    jpMsg.RunMethodjo("setAlias",Array(ctxt,"ceshi",jvo))

the logs
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **~de:main,44
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4j.object.JavaObject.createEvent(JavaObject.java:227)
    at anywheresoftware.b4j.object.JavaObject.CreateEvent(JavaObject.java:215)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at bjfhs.guo.com.main.afterFirstLayout(main.java:102)
    at bjfhs.guo.com.main.access$000(main.java:17)
    at bjfhs.guo.com.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 
Upvote 0

bjfhs

Active Member
Licensed User
Thank MarcoRome,your code work well! Also thank Erel for your help!

I'm a beginner of b4a,i think i will have more questions need your help in the future,Thank you very much!
 
Upvote 0
Top