Android Question you can pass this code to B4A

pablovidal

Member
Licensed User
Longtime User
http://stackoverflow.com/questions/8575717/android-handling-changing-apn-connection

B4X:
package com.slk.apnapp;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NewAPNActivity extends Activity {
/*
* Information of all APNs Details can be found in
* com.android.providers.telephony.TelephonyProvider
*/
public static final Uri APN_TABLE_URI = Uri
        .parse("content://telephony/carriers");
/*
* Information of the preferred APN
*/
public static final Uri PREFERRED_APN_URI = Uri
        .parse("content://telephony/carriers/preferapn");
private static final String TAG = "CHANGE_APN";
public static final String NEW_APN = "NewAPN";

private int getDafaultAPN() {
    Cursor c = this.getContentResolver().query(PREFERRED_APN_URI,
            new String[] { "_id", "name" }, null, null, null);
    int id = -1;
    if (c != null) {
        try {
            if (c.moveToFirst())
                id = c.getInt(c.getColumnIndex("_id"));
        } catch (SQLException e) {
            Log.d(TAG, e.getMessage());
        }
        c.close();
    }
    return id;

}

/*
* Set an apn to be the default apn for web traffic Require an input of the
* apn id to be set
*/
public boolean setDefaultAPN(int id) {
    boolean res = false;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();

    // See /etc/apns-conf.xml. The TelephonyProvider uses this file to
    // provide
    // content://telephony/carriers/preferapn URI mapping
    values.put("apn_id", id);
    try {
        resolver.update(PREFERRED_APN_URI, values, null, null);
        Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",
                "apn" }, "_id=" + id, null, null);
        if (c != null) {
            res = true;
            c.close();
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }
    return res;
}

private int checkNewAPN() {
    int id = -1;
    Cursor c = this.getContentResolver().query(APN_TABLE_URI,
            new String[] { "_id", "name" }, "name=?",
            new String[] { NEW_APN }, null);
    if (c == null) {
        id = -1;
    } else {
        int record_cnt = c.getCount();
        if (record_cnt == 0) {
            id = -1;
        } else if (c.moveToFirst()) {
            if (c.getString(c.getColumnIndex("name")).equalsIgnoreCase(
                    NEW_APN)) {
                id = c.getInt(c.getColumnIndex("_id"));
            }
        }
        c.close();
    }
    return id;
}

public int addNewAPN() {
    int id = -1;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", NEW_APN);
    values.put("apn", NEW_APN);

    /*
    * The following three field values are for testing in Android emulator
    * only The APN setting page UI will ONLY display APNs whose 'numeric'
    * filed is TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC. On
    * Android emulator, this value is 310260, where 310 is mcc, and 260
    * mnc. With these field values, the newly added apn will appear in
    * system UI.
    */
    values.put("mcc", "310");
    values.put("mnc", "260");
    values.put("numeric", "310260");

    Cursor c = null;
    try {
        Uri newRow = resolver.insert(APN_TABLE_URI, values);
        if (newRow != null) {
            c = resolver.query(newRow, null, null, null, null);

            // Obtain the apn id
            int idindex = c.getColumnIndex("_id");
            c.moveToFirst();
            id = c.getShort(idindex);
            Log.d(TAG, "New ID: " + id + ": Inserting new APN succeeded!");
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }

    if (c != null)
        c.close();
    return id;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int id = checkNewAPN();
    int default_id = getDafaultAPN();

    if (id == -1) {
        id = addNewAPN();
    }

    if (setDefaultAPN(id)) {
        Log.i(TAG, NEW_APN
                + " set new default APN successfully and Default id is "
                + id);
    }
    if (setDefaultAPN(default_id)) {
        Log.i(TAG,
                NEW_APN
                        + " set previous default APN successfully and Default id is "
                        + default_id);
    }

}
}
 

pablovidal

Member
Licensed User
Longtime User
Codigo para cambiar el APN desde la App,
Code from change APN from app.

Solo lo he probado con Android 2.3

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
Private cr As ContentResolver
Type Apns ( nId As Long, cName As String, cApn As String  )
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Main.lApp = True
    If FirstTime Then
        cr.Initialize("cr")
    End If

      If SeekApn( "claro1" ) = False Then
          PutApn( "claro1", "ba.amx", "370", "02" )
    End If

    If SeekApn( "claro2" ) = False Then
          PutApn( "claro2", "internet.ideasclaro.com.do", "370", "02" )
    End If

      If SeekApn( "orange" ) = False Then
          PutApn( "orange", "orangenet.com.do", "370", "01" )
    End If

    ListApns
    GetDefaultApn

    Activity.Finish

End Sub

Sub Activity_Resume
    Main.lApp = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Main.lApp = False
End Sub

Sub ListApns

Dim oUri As Uri
Dim oList As List
Dim oBrow As List
Dim nRes As Long
Dim oApn As Apns

oUri.Parse("content://telephony/carriers")

oList.Initialize
oBrow.Initialize

Dim crsr As Cursor = cr.Query(oUri, Array As String( "_id", "name","apn","type" ), "", Null, "" )

  
    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim oApn As Apns
      
        oApn.nId  = crsr.GetLong(  "_id"  )
        oApn.cName = crsr.GetString( "name" )
        oApn.cApn  = crsr.GetString( "apn"  )
      
        If oApn.cName.ToLowerCase = "claro" OR oApn.cName.ToLowerCase = "claro1" OR oApn.cName.ToLowerCase = "orange" OR oApn.cName.ToLowerCase = "claro2" Then
            oList.Add( oApn )
            oBrow.Add( oApn.cName )
        End If

    Next

    nRes = InputList(oBrow, "Seleccione Apn", -1)
    If nRes <> DialogResponse.CANCEL Then
        oApn = oList.Get( nRes )
        SetDefaultApn( oApn.nId )
    End If

End Sub


Sub SeekApn( cApn As String ) As Boolean

Dim oUri As Uri
Dim oApn As Apns

oUri.Parse("content://telephony/carriers")

Dim crsr As Cursor = cr.Query(oUri, Array As String( "_id", "name","apn","type" ), "", Null, "" )

    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
        Dim oApn As Apns
      
        oApn.nId  = crsr.GetLong(  "_id"  )
        oApn.cName = crsr.GetString( "name" )
        oApn.cApn  = crsr.GetString( "apn"  )
      
        If oApn.cName.ToLowerCase = cApn.ToLowerCase Then
            Return True
        End If

    Next
  
    Return False
  
End Sub


Sub SetDefaultApn( nId As Int )

  Dim values As ContentValues
  Dim uri1 As Uri : uri1.Parse("content://telephony/carriers/preferapn")

  values.Initialize
  values.PutInteger("apn_id", nId)
  cr.Insert(uri1, values)

End Sub


Sub GetDefaultApn
Dim oUri As Uri : oUri.Parse("content://telephony/carriers/preferapn")
Dim crsr As Cursor = cr.Query(oUri, Array As String( "_id", "name","apn","type" ), "", Null, "" )

    For i = 0 To crsr.RowCount - 1
        crsr.Position = i
      
        Dim cName As String = crsr.GetString( "name" )
        Dim cApn  As String = crsr.GetString( "apn"  )
  
    Next

    Msgbox( cName & " " & cApn, ":: Lotenet ::")

End Sub

Sub PutApn( cName As String, cApn As String, cMcc As String, cMnc As String )

  Dim values As ContentValues
  Dim uri1 As Uri : uri1.Parse("content://telephony/carriers")

  values.Initialize
  values.PutString("name", cName )
  values.PutString("apn" , cApn )
  values.PutString("mcc" , cMcc )
  values.PutString("mnc" , cMnc )
  values.PutString("numeric", cMcc & cMnc )

  cr.Insert(uri1, values)

End Sub
 
Last edited:
Upvote 0
Top