Home > Software engineering >  Add queries in runtime possible?
Add queries in runtime possible?

Time:07-17

is it possible to add queries in runtime? I know it is possible to change configurations and permissions but e.g if I want my app to interact with data of any app on my phone which is possible by having the packagename. But after API 30 I need to specify Apps I want to interact with as queries in the AndroidManifest.xml, or use the QUERY_ALL_PACKAGES permission which I don't want to use. Here is an Example how I imagined it: User enters my app and wants the icon of a specific app on his phone. Then enters the package name and clicks ok and now he can see the icon of a certain app.

CodePudding user response:

is it possible to add queries in runtime?

You appear to be referring to <queries> elements in the manifest. If so, then no, that is not possible.

But after API 30 I need to specify Apps I want to interact with as queries in the AndroidManifest.xml, or use the QUERY_ALL_PACKAGES permission which I don't want to use

Or, you specify Intent patterns of relevance in <queries>.

User enters my app and wants the icon of a specific app on his phone. Then enters the package name and clicks ok and now he can see the icon of a certain app.

First, approximately 0% of your users know package names. A more user-friendly UI would be to present the member with a list to choose from.

Second, it is unclear what you mean by "the icon of a certain app". If you mean "a launcher icon from a certain app", then basically you want similar capabilities to a launcher: show a list (or grid or whatever) of launcher icons for the user to choose from, after which you can do something with the chosen icon.

For that scenario, this should suffice:

  <queries>
    <intent>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent>

  </queries>

You can then use queryIntentActivities() to find the launcher activities on the device:

private val LAUNCHER_INTENT =
  Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)

data class MainViewState(
  val launcherActivities: List<ResolveInfo>
)

class MainMotor(private val context: Context) : ViewModel() {
  private val _states = MutableLiveData<MainViewState>()
  val states: LiveData<MainViewState> = _states

  fun load(force: Boolean = false) {
    if (force || _states.value == null) {
      val pm = context.packageManager

      _states.value = MainViewState(
        launcherActivities = pm.queryIntentActivities(LAUNCHER_INTENT, 0)
      )
    }
  }
}

CodePudding user response:

Hmh... read a bit more about Androidmanifest.xmls and Queries and found out the App i planned to do is exactly sth Google doesnt want to have so.... But still thanks for the answers ^^

  • Related