Installing APK by sending Intent to another service in Android 10 - android

I've got a long running application in a kiosk mode, I will call it 'main app'.
Additionally to that, there is another app installed on a device which manages the updates, I will call it 'updater app'. It's a service running in the background + activity which I use for 'debugging' and testing.
Up until android 9, there was no issue with it. The background service would receive info about update, would download it, inform the main app about update ready and when the main app was in a state which would allow an update it would send an intent and start the update.
With Android 9 and 10 things got a bit messy.
I had to add additional permissions, had to add file provider but in the end I got it to work through my 'testing' activity where I push a button and the app will launch download manager and after a successful download, it will launch the installer of the app. Cool.
Unfortunately, this only works when called from the activity of the 'updater'.
If I switch back to my main app and call intent to launch the installation, there is nothing happening - no user prompt whatsoever. I mean: my app sends intent to start the installation and the only thing I can see in logcat is the following warning:
2021-05-28 21:48:33.997 31553-31553/<updater_app> I/Timeline: Timeline: Activity_launch_request time:3253066
2021-05-28 21:48:34.006 1590-1626/? I/ActivityTaskManager: START u0 {act=android.intent.action.VIEW dat=content://<updater_app>.provider/downloads/prod-e470aba2f-6.apk typ=application/vnd.android.package-archive flg=0x10000001 cmp=com.google.android.packageinstaller/com.android.packageinstaller.InstallStart} from uid 10235
2021-05-28 21:48:34.007 1590-1626/? W/ActivityTaskManager: Background activity start [callingPackage: <updater_app>; callingUid: 10235; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false; realCallingUid: 10235; isRealCallingUidForeground: false; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; isBgStartWhitelisted: false; intent: Intent { act=android.intent.action.VIEW dat=content://<updater_app>.provider/downloads/prod-e470aba2f-6.apk typ=application/vnd.android.package-archive flg=0x10000001 cmp=com.google.android.packageinstaller/com.android.packageinstaller.InstallStart }; callerApp: ProcessRecord{d1ea9eb 31553:<updater_app>/u0a235}]
I assume there are some more restrictions to take care of, but I don't know where to look for more info. I would be grateful for pointing in the right direction.

Related

How to prevent another package/activity from running on the device through my application?

I have an app which has system privileges. My app displays a UI when the device starts after reboot. Issue is, another activity (not part of my app) also launches, therefore backgrounding my activity.
//my activity starts
ActivityManager: START u0 {flg=0x10000000 cmp=com.company.myApp/.activities.MyActivity} from uid 10097 on display 0
..... stuff happening -> UI is being updated
// some other activity starts (automatically)
ActivityManager: START u0 {act=com.company.someOtherApp.Presentation.APL_RenderDocument flg=0x18000000 pkg=com.company.test cmp=com.company.test/.SomeOtherActivity (has extras)} from uid 10082 on display 0
// my activity goes into background.
MyActivity: onPause()
MyActivity: onStop()
Is there a way to prevent this other application from being created (or from being run)?
I'm looking into 2 particular methods
setComponentEnabledSetting()
Set the enabled setting for a package component (activity, receiver, service, provider). This setting will override any enabled state which may have been set by the component in its manifest.
setApplicationEnabledSetting()
Set the enabled setting for an application This setting will override any enabled state which may have been set by the application in its manifest. It also overrides the enabled state set in the manifest for any of the application's components. It does not override any enabled state set by setComponentEnabledSetting(ComponentName, int, int) for any of the application's components.
I don't quite understand the difference of if those methods can do what I want to achieve.
If an "enabled setting"/"enabled state" for a package/application is set to disabled, does that mean that application or package never gets invoked?
Is there another approach?
There are two ways as far I know
One is kill process
Process.killProcess( APP-PROCESS-ID )
Or you can kill background processes with activity manager liek this
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);
Declare this permission KILL_BACKGROUND_PROCESSES

Can't start the activity from the BroadcastReceiver in Android 9 or later

I am unable to start the activity from the Broadcastreceiver in Android 9 or later. I know this might be duplicate question but I didn't get the solution, so asking.
Here is my code:
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Please help me.
When I try running your code on an emulator with Android 10, I get the following Logcat entries:
2019-12-30 19:26:11.579 2048-2096/system_process I/ActivityManager: Start proc 5504:com.example.broadcastreceivertest/u0a133 for broadcast {com.example.broadcastreceivertest/com.example.broadcastreceivertest.MyReceiver}
2019-12-30 19:26:11.695 5504-5504/com.example.broadcastreceivertest E/TEST: onReceive()
2019-12-30 19:26:11.730 2048-5522/system_process I/ActivityTaskManager: START u0 {flg=0x10000000 cmp=com.example.broadcastreceivertest/.MainActivity} from uid 10133
2019-12-30 19:26:11.738 2048-5522/system_process W/ActivityTaskManager: Background activity start [callingPackage: com.example.broadcastreceivertest; callingUid: 10133; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false; realCallingUid: 10133; isRealCallingUidForeground: false; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; isBgStartWhitelisted: false; intent: Intent { flg=0x10000000 cmp=com.example.broadcastreceivertest/.MainActivity }; callerApp: ProcessRecord{5ddbd7 5504:com.example.broadcastreceivertest/u0a133}]
2019-12-30 19:32:12.313 2048-2095/system_process I/ActivityManager: Killing 5504:com.example.broadcastreceivertest/u0a133 (adj 985): empty #17
The interesting part is
W/ActivityTaskManager: Background activity start
Devices running Android 10 (API level 29) and higher have Restrictions on starting activities from the background. This means you can no longer start an Activity from a BroadcastReceiver. It is recommended to use a notification instead, so that users can start the Activity by tapping on the notification.
There are some exceptions to this behavior (see the linked page).
An easy way out may be to add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
to your Manifest file. (Note: when installing the app via Android Studio, you have to manually grant the permission once the app is installed)
That being said, it may be wiser to switch over to showing a notification - depending on your use case and your distribution channels.
Since it's possible to show even a full screen notification for the "problem API levels" 29 and higher, consider showing the Activity/ showing a notification in onReceive(), depending on the device's android version.

MIUI Permission Denied Activity KeyguardLocked

When application is in background and screen is locked,I'm unable to start activity, there is no exception or warning, onCreate() is just not called. I've been struggling against this problem for while, and I think I've finally found the source of it.
There is a line in logs:
D/com.android.server.am.ExtraActivityManagerService: MIUILOG- Permission Denied Activity KeyguardLocked: Intent { flg=0x14010000 cmp=com.trueconf.videochat/com.trueconf.gui.activities.Call (has extras) } pkg : com.trueconf.videochat uid : 10634
Is this why I can't start an activity? Also, how do I solve this, should I disable keyguard or something?
This is a special permission on MIUI that has to be enabled for apps.
You can find it in
Settings
Manage Apps
YOUR APP
Other permissions
Show on Lock screen
User must enable this manually. As far as I know, the best you can do is to guide the user to this settings menu directly by launching an intent (and probably showing some explainer text prior to this).
Intent looks like this
startActivity(new Intent("miui.intent.action.APP_PERM_EDITOR").putExtra("extra_pkgname", getPackageName()))
Make sure to try catch it as it can throw exceptions if activity can't be started (e.g. the device actually is not running MIUI or the intent is somehow not valid on the particular device/version)

How to override default call screen?

I want to launch my customized screen when user dials any number from my android app instead of default caller screen.
Generally speaking, in order to know how to override any default Activity, first you need to know the structure of the Intent that can launch the Activity.
Determining the structure of the Intent
Open Android Monitor (aka Logcat)
Filter the log to only show those matching the string "ActivityManager"
Launch the Activity that you want to override. In your case, launch the call screen
If the Activity can be overridden, you should see a log entry with "START...", copy that entry so that you don't lose it in the log. On my device, this entry was:
START u0 {act=android.intent.action.CALL dat=tel:xxxxxxxxxxx flg=0x10000000 cmp=com.android.server.telecom/.CallActivity (has extras)} from uid 10088 on display 0
This Intent is made up of
act - The Intent action
dat - The Intent data
cmp - The Intent component
Now you need to check if this Intent can launch the default dialer without specifying the component.
Checking if a default Activity can be overridden
adb shell
am start -a android.intent.action.CALL -d tel:xxxxxxxxxxx (fill in the number you want to test with)
If it launches the dialer, then, voila. You should be able to create an IntentFilter for your application, setting the action and the data appropriately. Then, the next time the user tries to make a call, it will ask the user which app they want to use.

startActivityForResult calling window killed before result returned

I have a launcher app that is attempting to add widgets. Most work fine, but if you have a configure screen, the widget is never added. The launcher calls
REQUEST_PICK_APPWIDGET = 9
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
and the configure activity is launched, but as its coming up i see this in the lolcat
W/WindowManager( 2096): Rebuild removed 7 windows but added 6
W/WindowManager( 2096): This window was lost: Window{42abf130 com.android.settings/com.android.settings.AppWidgetPickActivity paused=false}
The configure finishes and nothing happens because the appwidgetpicker is already dead.
The launcher never actually crashes, theres never anything bad that happens, just nothing happens. Its not the app itself that is crashing, its the systemappwindgetpicker afaik.
I don't really know what the problem is, but one of these threads might be of help to you:
Drop event when select OK to restart Launcher when ANR occurs
Android - problems with a multi-level activity chain
https://github.com/T3hh4xx0r/Hax-Launcher/commit/4b6d2d99fdef0ded0684688a2ac5ddeb2faa9f1d
Somewhere along the line, a variable was getting nullified. I just created a backup basically to prevent this from happening.

Categories

Resources