Java Question Get the PendingIntent result from Java Object code

Status
Not open for further replies.

phukol

Active Member
Licensed User
Longtime User
HI everyone!

Im using this code to check for dual sim and send sms using dual sim card. I just want to know how i can get the result of my messages sent using PendingIntent.I can already display the result using toast but i need to capture the said event. I want to know if i have sent the message successfully or an error was encountered and capture it using b4a code:

B4X:
#If JAVA
     import java.util.List;
     import java.util.ArrayList;
    import android.app.Activity;
    import android.telephony.SubscriptionManager;
    import android.telephony.SubscriptionInfo;
    import android.telephony.SmsManager;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.widget.Toast;
    
    private static final String SMS_SENT = "SMS_SENT";
    private static final String SMS_DELIVERED= "SMS_DELIVERED";
    
    public static ArrayList<Integer> sim(Activity activity) {
        ArrayList<Integer> simCardList = new ArrayList();
        SubscriptionManager subscriptionManager  = SubscriptionManager.from(activity);
        List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
    
        for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
            int subscriptionId = subscriptionInfo.getSubscriptionId();
            simCardList.add(subscriptionId);
        }
        
        return simCardList;
    }

    public static ArrayList<String> network(Activity activity) {
        ArrayList<String> simCardList = new ArrayList();
        SubscriptionManager subscriptionManager  = SubscriptionManager.from(activity);
        List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
    
        for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
            String subscriptionId = subscriptionInfo.getCarrierName().toString();
            simCardList.add(subscriptionId);
        }
        
        return simCardList;
    }
    
    public  void sendSMS(Activity activity,Integer simID,String strDest,String strMsg) {
        ArrayList<Integer> simCardList = new ArrayList();
        SubscriptionManager subscriptionManager  = SubscriptionManager.from(activity);
        List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
    
        for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
            int subscriptionId = subscriptionInfo.getSubscriptionId();
            simCardList.add(subscriptionId);
        }
        

         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);

         PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context  arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SMS_SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context  arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;                       
                }
            }
        }, new IntentFilter(SMS_DELIVERED));   
        
        int smsToSendFrom = simCardList.get(simID); //assign your desired sim to send sms, or user selected choice
        SmsManager.getSmsManagerForSubscriptionId(smsToSendFrom).sendTextMessage(strDest, null, strMsg, sentPI, deliveredPI); //use your phone number, message and pending intents
    }
    
#End If
 

DonManfred

Expert
Licensed User
Longtime User
Instead of generating the Toast you could raise an Event in B4A and give the Value to this event.
 

phukol

Active Member
Licensed User
Longtime User
Thank you. Any sample DonManFred? I just found the code snippet online and tried using it in my b4A app. However i just want to capture the intent.
 

phukol

Active Member
Licensed User
Longtime User
THanks again. But i am not an advanced java coder. I need to create an event in the java code right and call this using javaobject CreateEvent right? But i dont have any idea on how i can do this.
 

phukol

Active Member
Licensed User
Longtime User
Thanks for that useless reply. Thats why i am asking. i have searched the forum and looked up all i can for that. Anyway, if that's all the help i can get thank you Sir!

PS.

Method_636.png
JavaObject. CreateEvent (Interface As String, EventName As String, DefaultReturnValue As Object) As Object

Creates an instance of the interface and binds it to the object.
Interface - The full interface name.
EventName - The prefix of the event sub.
DefaultReturnValue - This value will be returned if no value was returned from the event sub. This can happen if the Activity is paused for example.

For example:
Sub Activity_Create(FirstTime As Boolean)
Dim b As Button
b.Initialize("")
Activity.AddView(b, 0, 0, 200dip, 200dip)
Dim jo As JavaObject = b
Dim e As Object = jo.CreateEvent("android.view.View.OnTouchListener", "btouch", False)
jo.RunMethod("setOnTouchListener", Array As Object(e))
End Sub

Sub btouch_Event (MethodName As String, Args() As Object) As Object
Dim motion As JavaObject = Args(1) 'args(0) is View
Dim x As Float = motion.RunMethod("getX", Null)
Dim y As Float = motion.RunMethod("getY", Null)
Log(x & ", " & y)
Return True
End Sub

and i am having trouble finding sample java code for the ontouchlistener part.

I respect you DonManFred for all your contributions, but please respect noobs like me who respect experts like you. Thank you
 
Status
Not open for further replies.
Top