Take a look at the attached example.
In the manifest editor i add a BroadcastReceiver for the action android.intent.action.ACTION_POWER_CONNECTED.
The BroadcastReceiver is disabled by default in the manifest:
AddReceiverText(svc_power, <intent-filter><action android:name="android.intent.action.ACTION_POWER_CONNECTED"/></intent-filter>)
SetReceiverAttribute(svc_power, android:enabled, "false")
svc_power logs the action of the starting intent and shows a toast
if the starting intent action is android.intent.action.ACTION_POWER_CONNECTED:
Sub Process_Globals
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
Dim Action As String=StartingIntent.Action
Select Action
Case "android.intent.action.ACTION_POWER_CONNECTED"
Log("action=android.intent.action.ACTION_POWER_CONNECTED")
ToastMessageShow("svc_power started, action=android.intent.action.ACTION_POWER_CONNECTED", False)
End Select
StopService("")
End Sub
Sub Service_Destroy
End Sub
The Main activity code is:
Sub Process_Globals
' see http://developer.android.com/reference/android/content/pm/PackageManager.html
' This component is in its default enabled state (as specified in its manifest).
Private ENABLED_STATE_DEFAULT As Int=0
' This component has been explicitly disabled, regardless of what it has specified in its manifest.
Private ENABLED_STATE_DISABLED As Int=2
' This component has been explicitly enabled, regardless of what it has specified in its manifest.
Private ENABLED_STATE_ENABLED As Int=1
Private BROADCAST_RECEIVER_FULLY_QUALIFIED_NAME As String="b4a.example.svc_power$svc_power_BR"
End Sub
Sub Globals
Private JavaObject1 As JavaObject
Private Label1 As Label
Private ToggleButton1 As ToggleButton
End Sub
Sub Activity_Create(FirstTime As Boolean)
JavaObject1.InitializeContext
Activity.LoadLayout("Main")
End Sub
Sub Activity_Resume
UpdateUI
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ToggleButton1_CheckedChange(Checked As Boolean)
Dim EnabledState As Int
If Checked Then
EnabledState=ENABLED_STATE_ENABLED
Else
EnabledState=ENABLED_STATE_DISABLED
' as the broadcast receiver is disabled by default in the manifest, you could instead set the default value instead of disabled value:
' EnabledState=ENABLED_STATE_DEFAULT
End If
JavaObject1.RunMethod("setComponentEnabledSetting", Array As Object(BROADCAST_RECEIVER_FULLY_QUALIFIED_NAME, EnabledState))
UpdateUI
End Sub
Sub UpdateUI
Dim EnabledState As Int=JavaObject1.RunMethod("getComponentEnabledSetting", Array As Object(BROADCAST_RECEIVER_FULLY_QUALIFIED_NAME))
Select EnabledState
Case ENABLED_STATE_DEFAULT
' svc_power is disabled by default (in the manifest)
Label1.Text="ENABLED_STATE_DEFAULT"
ToggleButton1.Checked=False
Case ENABLED_STATE_DISABLED
Label1.Text="ENABLED_STATE_DISABLED"
ToggleButton1.Checked=False
Case ENABLED_STATE_ENABLED
Label1.Text="ENABLED_STATE_ENABLED"
ToggleButton1.Checked=True
Case Else
Label1.Text="EnabledState="&EnabledState&CRLF&"This state not anticipated..."
ToggleButton1.Enabled=False
End Select
End Sub
#If JAVA
import android.content.ComponentName;
import android.content.pm.PackageManager;
public int getComponentEnabledSetting(String fullyQualifiedClassName){
ComponentName componentName = new ComponentName(this, fullyQualifiedClassName);
return this.getPackageManager().getComponentEnabledSetting(componentName);
}
public void setComponentEnabledSetting(String fullyQualifiedClassName, int enabledState){
ComponentName componentName = new ComponentName(this, fullyQualifiedClassName);
this.getPackageManager().setComponentEnabledSetting(componentName, enabledState, PackageManager.DONT_KILL_APP);
}
#End if
Compile and install the example.
You'll see a Label display the text "ENABLED_STATE_DEFAULT", a ToggleButton will be unchecked.
The BroadcastReceiver is in it's default state - it is not enabled.
You can use the ToggleButton to change the BroadcastReceiver's state from 'default' to either 'enabled' or 'disabled'.
Connect your device to power and you'll see a Toast message if the BroadcastReceiver is enabled, otherwise you'll see nothing.
Martin.