PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → WM18 Java
WM18 Java
Débuté par iso, 16 oct. 2014 12:38 - 2 réponses
Posté le 16 octobre 2014 - 12:38
Hello all,

Hoping someone can help me convert this java script to run in Windev Mobile 18:


import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class PhoneStateMonitor extends PhoneStateListener {
Context context;

public PhoneStateMonitor(Context context) {
super();
// TODO Auto-generated constructor stub
this.context=context;
}

//This Method Automatically called when changes is detected in Phone State
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);

Toast.makeText(context, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed
//Checking The phone state
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE: //Phone is in Idle State
Toast.makeText(context, "Phone State is IDLE", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING: //Phone is Ringing
Toast.makeText(context, "Phone State is RINGING", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //Call is Received
Toast.makeText(context, "Call State is OFFHOOK",Toast.LENGTH_LONG).show();
break;
}
}
}



I can get this part to work:

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;


public static void check_state(int state, String incomingNumber)

{
// TODO Auto-generated method stub
Context context2 = getApplicationContext();


//this bit is missing the loop part where it checks for any changes on state


Toast.makeText(context2, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed
//Checking The phone state
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE: //Phone is in Idle State
Toast.makeText(context2, "Phone State is IDLE", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING: //Phone is Ringing
Toast.makeText(context2, "Phone State is RINGING", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //Call is Received
Toast.makeText(context2, "Call State is OFFHOOK",Toast.LENGTH_LONG).show();
break;
}
}



hope someone can advise on this.

many thanks iso
Posté le 24 octobre 2014 - 12:52
Really appreciate you help on this Danny here's his amazing reply, hope it can help out others in the future:


Hi,

I just had a little time to take a look at this and there are a few things missing.

First, I had currently only success with static functions. When I try to use non static classes and try to instantiate them I got compilation errors all over the place. So currently in my WM (Android) applications I am doing every thing in static classes and procedures for the Java code. I'll have to ask PC-Soft on how I would accomplish the dynamic Java classes and functions !

So What did I had to change to get things working !

1) Change the class to a static class. I have a global functions with only imports and static class definitions. This functions is never run but because it is compiles it knows the classes and uses the imports. This makes things easier to separate things and not repeating the imports in every function. So these lines are normally in my "InitClasses" global function.

import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.widget.Toast; public static class PhoneStateMonitor extends PhoneStateListener { Context context; public PhoneStateMonitor(Context context) { super(); // TODO Auto-generated constructor stub this.context=context; Toast.makeText(context, "Init PhoneStateMonitor",Toast.LENGTH_LONG).show(); } //This Method Automatically called when changes is detected in Phone State // Various states are - CALL_STATE_IDLE, CALL_STATE_OFFHOOK, and CALL_STATE_RINGING // IDLE – when there is no incoming call // OFFHOOK – when the line is busy // RINGING – when call is incoming public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); Toast.makeText(context, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed //Checking The phone state switch(state) { case TelephonyManager.CALL_STATE_IDLE: //Phone is in Idle State Toast.makeText(context, "Phone State is IDLE", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_RINGING: //Phone is Ringing Toast.makeText(context, "Phone State is RINGING", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_OFFHOOK: //Call is Received Toast.makeText(context, "Call State is OFFHOOK",Toast.LENGTH_LONG).show(); break; } } }
We now have the definitions in place, but that does not mean it is activated. It is a dead function !

2) Now we need to use this class somewhere so the events of the CallState are redirected en the toast's popup. I have written an other function for this (in the same global procedures file)

public static void JRegisterPhoneStateListener() { // // needs android.permission.READ_PHONE_STATE // // Get the Context if the WM application Context tmpContext = getContexteApplication(); // Get the Telephone manager TelephonyManager TelephonyMgr = (TelephonyManager)tmpContext.getSystemService(tmpContext.TELEPHONY_SERVICE); // we need to register a listener to receive notification about the changes in telephony states // If any of the state is changed the PhoneStateMonitor.OnCallStateChanged is triggerd TelephonyMgr.listen(new PhoneStateMonitor(tmpContext), PhoneStateListener.LISTEN_CALL_STATE); }
We are getting the current WM contect and using it to get to the active telephonemanager, there we hookup a listener and provide our PhonestateMonitor class as a callback.
But this is a function, so we have to call this somewhere !

You have to check if your application has permission to READ_PHONE_STATE, because it is a Java function this is not automatically detected by WM.
In one of the steps in the compilation wizard you can check or add this permission to the list if it is not already present.

3) Activate the Listener by calling the JRegisterPhoneStateListener

The class and imports are active by compilation and the function can be called in the project init code or in the Global declarations of your main window

IF NOT InTestMode() THEN JRegisterPhoneStateListener() END
When you put this code in the correct place in your program, this will give all the toastmessages when the state of the telephone is changing. I have tested this by putting these functions in one of my android programs, and the toast where displaying correctly. You can extend on this to build the application functionality.
You probably know that you can call WL functions from your Java code, so instead of displaying a toast you can call a WL functions that does your actions.

Voila, I hope this will work in your project too. And if you will be so kind in publishing the solution to the forum so others can benefit from this info too.
Correct any spelling errors also <img src="/NG2013_WEB/ui/smiley/1.gif" align=absmiddle border=0 alt=":-)">

Have a nice day
Posté le 24 octobre 2014 - 15:52
Iso,

Hope you get your solution working with the tips I send you ! <img src="/NG2013_WEB/ui/smiley/1.gif" align=absmiddle border=0 alt=":)">

Bye
Danny