Pubblico un pò di frammenti che negli anni ho raccolto su internet che possono essere utili
Sviluppatori B4X e altri linguaggi minori ....
Forum dedicato agli sviluppatori di B4X, PHP, Xamarin
Non sei connesso Connettiti o registrati
Sub ForceGallery (Dir As String, FileName As String)
Dim Phone As Phone
If Phone.SdkVersion<19 Then
Dim i As Intent
i.Initialize("android.intent.action.MEDIA_MOUNTED", "file://" & File.Combine(Dir, FileName))
Phone.SendBroadcastIntent(i)
Else
Dim context As JavaObject
context = context.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
Dim paths() As String = Array As String(File.Combine(Dir,FileName)) 'files to scan
Dim mediaScanner As JavaObject
mediaScanner.InitializeStatic("android.media.MediaScannerConnection")
mediaScanner.RunMethod("scanFile", Array As Object(context, paths, Null, Null))
End If
End Sub
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:pathPattern=".*\\.epub" />
<data android:mimeType="application/epub+zip" />
</intent-filter>)
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim In As Intent = Activity.GetStartingIntent
Dim Parametri As String = In.GetData
End Sub
Dim jo As JavaObject
jo.InitializeContext
Dim r As Reflector
r.Target = jo.RunMethod("getSystemService", Array("audio"))
r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "device"), Array As String("java.lang.int", "java.lang.int", "java.lang.String"))
Dim i As Intent
i.Initialize(i.ACTION_VIEW,"/mnt/sdcard/Data/DailyProd.pdf")
i.SetType("application/pdf")
Public Sub GetBatteryWirelessCharging As Boolean
Dim Context As JavaObject
Context.InitializeContext
Dim BatteryManager As JavaObject = Context.RunMethod("getSystemService",Array("batterymanager"))
Dim IntentFilter As JavaObject
IntentFilter.InitializeNewInstance("android.content.IntentFilter",Array("android.intent.action.BATTERY_CHANGED"))
Dim BatteryStatus As Intent = Context.RunMethod("registerReceiver",Array(Null,IntentFilter))
' Dim Status As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_STATUS"))
Dim ChargePlug As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_PLUGGED"))
' Dim USBCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_USB")
' Dim ACCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_AC")
' Dim WirelessCharge As Boolean = BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
'
Return ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
'2 = USB , 4 = Wireless
End Sub
' NEL MANIFEST
AddPermission("android.permission.WRITE_SETTINGS")
'NEL CODICE
Sub screenBrightness( brightnessInt As Int) '0..255
' Original code
' android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
' android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);
Dim J As JavaObject
J.InitializeContext
J.RunMethod("setBrightness",Array(J,brightnessInt))
End Sub
#if JAVA
import android.content.Context;
public void setBrightness(Context mContext, int brightnessInt){
android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);
}
#End If
public Sub ISO_6346_Ck(CtrNum As String) As Boolean
Dim acc, ax, checkdigit As Int
Dim remx, s, i As Int
Dim b As Boolean
Dim Indice() As String= Array As String("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
Dim Value() As Int = Array As Int (10,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,31,32,34,35,36,37,38)
Dim M As Map
M.Initialize
' Carica il vettore dei valori alfanumerici -> Star-Dust mode :)
For i= 0 To Indice.Length - 1
M.Put(Indice(i),Value(i))
Next
acc = 0
s = 0
' Esegue l'algoritmo per il check dei primi 10 valori
For i = 0 To 9
' L'algoritmo prevede la moltiplicazione con 2^n e per n da 0 a 9
s = Power(2, i)
' Le prime quattro sono lettere
If i < 4 Then
ax = M.Get(Indice(Asc(CtrNum.CharAt(i))-65)) * s
Else
' I restanti 6 sono numeri
ax = (Asc(CtrNum.CharAt(i)) - 48) * s
End If
acc = acc + ax
Next
remx = (Floor(acc) Mod 11) Mod 10
' Estrazione check digit e conversione in numero
checkdigit = Asc(CtrNum.CharAt(10))-48
' Controllo tra calcolato e inserito (ultima cifra)
If (CtrNum.Length = 11) And (remx = checkdigit) Then
b = True
End If
Return (b)
End Sub
Sub IntToDIP(Integer As Int) As Int
Dim r As Reflector
Dim scale As Float
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
r.Target = r.RunMethod("getDisplayMetrics"))
scale = r.GetField("density")
Dim DIP As Int
DIP = Integer * scale + 0.5
Return DIP
End Sub
'we need this function to count the lines
Sub getLineCount(TargetView As Object) As Int
Dim source = TargetView As JavaObject
Return source.RunMethod("getLineCount", Null)
End Sub
'Sets the cursor visible or hides it
Sub setCursorVisible(edt As EditText, Visible As Boolean)
Dim jo = edt As JavaObject
jo.RunMethod("setCursorVisible", Array As Object(Visible))
End Sub
-----------------------------------------------------------------
'Sets the text selectable or not selectable
Sub setTextIsSelectable(edt As EditText, Selectable As Boolean)
Dim jo = edt As JavaObject
jo.RunMethod("setTextIsSelectable", Array As Object(Selectable))
End Sub
'Selects the text between the two indexes.
Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
Dim jo = edt As JavaObject
jo.RunMethod("setSelection", Array As Object(StartIndex, EndIndex))
End Sub
'Gets the selection start index
Sub getSelectionStart(edt As EditText) As Int
Dim jo = edt As JavaObject
Return jo.RunMethod("getSelectionStart", Null)
End Sub
'Gets the selection end index
Sub getSelectionEnd(edt As EditText) As Int
Dim jo = edt As JavaObject
Return jo.RunMethod("getSelectionEnd", Null)
End Sub
All UPCASE
EditText.InputType=Bit.Or(EditText.INPUT_TYPE_TEXT,4096)
First Upcase
EditText.InputType=Bit.Or(EditText.INPUT_TYPE_TEXT,8192)
Email
EditText.InputType=Bit.Or(EditText.INPUT_TYPE_TEXT,0x00000020)
Password
EditText.InputType=Bit.Or(EditText.INPUT_TYPE_TEXT,0x00000080)
If Regex.IsMatch(".+@.+\..+", req.GetParameter("email")) = False Then
resp.SendError(500, "Invalid email address.")
End If
Sub CloseActivities
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT")>15 Then
Dim jo As JavaObject
jo.InitializeContext
jo.RunMethod("finishAffinity", Null)
End If
End Sub
Sviluppatori B4X e altri linguaggi minori .... » B4A » (B4A) Librerie aggiuntive e frammenti di codice » FRAMMENTI DI CODICE
Permessi in questa sezione del forum:
Non puoi rispondere agli argomenti in questo forum.
Nessun utente |
Commenti: 25
Commenti: 8
Commenti: 2