Android Question It worked on Android 10

steveo

New Member
I have copied and pasted together some code to test if MicroDroid can send Intents to "turn on/off" an app.
Using 'android.intent.action.SENDTO' with extra string data, the test worked well on an android 10 phone.
If the app was already running then sending an intent would "turn it off".
If the app was not running then sending an intent would "turn it on".
When I tried the setup on a phone running android 13, things didn't go so well.
I could get the turn on function but not the off function.

intent test:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume
    Dim i As Intent
    i = Activity.GetStartingIntent
    If i <> Null Then
        If i.HasExtra("Settings") Then
            If i.GetExtra("Settings") = "stuff" Then
                Sleep(1000)
                CloseActivity
            End If
        End If
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub CloseActivity
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("finishAndRemoveTask", Null)
End Sub

intent filter:
<intent-filter>
    <action android:name="android.intent.action.SENDTO" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />   
</intent-filter>

I assumed finishAndRemoveTask is ok for android 13. It was discussed in an Android developers article, last updated 2023-03-21.
I searched for permission changes but had no luck finding any correlation to what I was trying to do.

Could somebody help me with the magic "Why is it so" and/or some topics to read and learn.
 
Solution
Thank you for your reply.

Erel said:

You must add something like IsRelevantIntent
For this test I assumed the relevancy of the intent was handled by checking the extra data named "Settings" with value "stuff". In Android 10 I debbuged this with ToastMessageShow and ExtrasToString to prove to myself that this extra data was received correctly. I had not done the same debbuging in Android 13. If I had I would have noticed that the string "Settings " had a silent space on the end. This was the reason for my failure.

Erel said :

Where is the code that starts the activity?
I used the term "turn it on" because I don't understand how intent mechanics works. If the app was not running or had not been...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

steveo

New Member
Thank you for your reply.

Erel said:

You must add something like IsRelevantIntent
For this test I assumed the relevancy of the intent was handled by checking the extra data named "Settings" with value "stuff". In Android 10 I debbuged this with ToastMessageShow and ExtrasToString to prove to myself that this extra data was received correctly. I had not done the same debbuging in Android 13. If I had I would have noticed that the string "Settings " had a silent space on the end. This was the reason for my failure.

Erel said :

Where is the code that starts the activity?
I used the term "turn it on" because I don't understand how intent mechanics works. If the app was not running or had not been started, then sending an intent from MacroDroid would result in the running of ToastMessageShow in both Activity_Create and Activity_Resume.

Erel said :

On Android 10+ you cannot start an activity from a service without the "draw over apps" permission.
I was able to add the permission : android.permission.SYSTEM_ALERT_WINDOW (aka 'draw over other apps') and allow it on both my Android 10 and 13 phones. This didn't appear to change the previous operation of my code. But it did add another entry to the vast list of things I have learnt.

I spent 5 days trying to get either Eclipse or Android Studio to run this, my first Android test app. In comparison B4X is an absolute pleasure.
Thankyou.
 
Upvote 0
Solution
Top