Restart native app after crash - android

In android 4.4 and below, who loads a native application (/system/bin/*) at startup, I think that the file init.rc is responsible, it is correct?
Then if a native application crashes (for example /system/bin/mediaserver) it restart automatically, then the question is: who is responsible for the application restart? there is a file?

ActivityManagerService restarts the native apps.
There's usually some chatter in logcat when an app is restarted by the activity manager service, in the normal log and/or event log
(logcat -b events).
More Info:
If you see the code of ActivityManagerService.finishForceStopPackageLocked() method, this method fires an Intent with action Intent.ACTION_PACKAGE_RESTARTED.
And it is called from various methods like:
ActivityManagerService.forceStopPackage()
IPackageDataObserveronRemoveCompleted.onRemoveCompleted()
So internally there's an PackageDataObserver implemented in ActivityManagerService, which observes if any package is removed, and if it's need to be restarted, an intent is fired with the action Intent.ACTION_PACKAGE_RESTARTED
And every package is forced closed using ActivityManagerService.forceStopPackage(), it knows which package to restart.
Hope it clears the doubt.

Related

Add Debugger in Android Studio after app is closed?

Is it possible to attach the Android debugger of Android Studio again in order to check Break point after app has been closed ?
You may be able to attach a debugger if the app process is invisible but still alive. If a process is terminated or not is up to the Android OS.
However you will not be able to "check" a breakpoint as breakpoint only work if the code the breakpoint is contained is executed. Breakpoints therefore only work interactively - if you attach to a process after a breakpoint has been passed it is impossible to extract information regarding the process state (variables, ..) at the time the breakpoint was passed, because it is no longer available.
You are probably trying to open the app from a notification or service when your app is closed or has been removed from the background by the Android system.
Here is a nice workaround for that.
Make a new class named Notification.class and extend Application class to it as shown below.
package com.example.app;
import android.app.Application;
public class Notification extends Application {
#Override
public void onCreate() {
super.onCreate();
}
}
After this, in your AndroidManifest.xml file, make the following changes.
<application
android:name=".NotificationClass"
.
.
android:usesCleartextTraffic="true">
</application>
After this whenever you receive a notification for your app and your app is not in background or foreground, you will be able to attach the debugger from Android Studio before clicking on the notification.
Hope it helps. :)

How can an apps application class run at once the device starts?

How can an apps application class run at once the device starts?
Hello,
I have an application where the application class is running it's onCreate, but no activites on device startup. And i don't mean when the BOOT broadcast is sent, i mean at once the device is started.
I can see this since i've added file logging to the appliation class, but i cannot see that it comes any further (into any activities).
My manifest has
No receivers
No services with their own process id
Is there anything else that could start my application class on device start?
When i click and start the application on my device, i see logs in the application class again, but this time it has another process id than it had.
Thanks in advance!
===================
ok, so after commenting out all obvious code and manifest entries, i finally set my Sync Adapter Service to enabled=false, and then the problem did not occur. Is this normal behaviour of a sync adapter service?

The application sometimes restarts multiple times after shutdown

I have a problem, users report that when the application shuts down (ending the process), the app restarts and user must shuts it down again and the app restarts again. Sometimes even 4x ...
How is it possible? I will close all the services & activities that have been started and I will terminate the whole process ...
I've noticed that only users with android 7 report it to me. It's never happened to me (android 5).
It is the same restart as if the activity is an error, just an exception and a restart. But Fabric.io tool has no record of any errors ... so I do not know what could happen, does anyone have any idea?
Add a Log.d statement into your onCreate in the Application class.
Extent the application class
public class TheApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Log.d("restart", "My App is restarting");
}
}
and in the manifest
android:name=".TheApplication" <-- make sure the package is correct.
Install the app adb install yourapp.apk
open a terminal
adb logcat restart *:S
then click on your app and watch the terminal.
This way you don't have to run it in debug mode and it runs like a regular app.
If you see it logging the app restarting several times maybe you can put in a trace in places (loging) like put one in the onDestroy of the main launcher class.
hope you figure it out.
It could happen from the 3rd party library that you includes into the project.
Some of them might have a service to trigger something intervally and that wake up the app the couple times.
This issue happened before on the app that i've been working on where the app keep waking up after that particular library get included.
Please check for any service that running in the background.

Android : can native code get broadcast intent from android system? [duplicate]

This question already has answers here:
Listen to own application uninstall event on Android
(3 answers)
Closed 7 years ago.
Recently i've seen a funny app - Photo Wonder.
When this app is uninstalled, it shows a web survey page asking for the reason of app uninstall. Now, here is the problem.
As far as I know, after an app has been removed, the system broadcasts ACTION_PAKAGE_REMOVED intent.
But this funny app was able to show my the web page although the official doc says
"The package that is being installed does not receive this Intent."
Anyhow, I could find a process checking some kind of status of the app.
Now here is the question. Can the native app catch the broadcasted intent from android system?
If it is possible, please let me know how! :-(
I believe I've got the main idea of how they did it. Here is the pieces of the puzzle.
Any Android application can start a process by calling Runtime.exec() function.
Runtime.getRuntime().exec("chmod 755 '/data/data/my.app/files'/native_code");
After this line of code gets executed there is another process spawned. This process runs under the same linux user as the application itself.
When a user opens Settings -> Apps -> My App and presses "Force stop" button, main application process gets killed, but the process hosting native program (see above) still runs. I personally believe this is a security issue and I am going to report it back to AOSP.
Such native program can run infinitely and do nothing - just sleeping. But before going to sleep, it registers a termination signal handler which will be called when process is about to be terminated by the system.
int main(void) {
signal(SIGTERM, termination_handler);
while(1) {
sleep(10);
}
}
void termination_handler(int sig) {
// handle termination signal here
}
Now you should already know what the last piece is, right? My native termination_handler should be able to launch a browser. I didn't try this in code, but I assume this is possible, because I can do it using adb shell as following
adb shell am start -a android.intent.action.VIEW -d http://www.google.com
Now back to the question about how Dolphin Browser does it. Install the app and launch it at least once. Once started, it registers a native uninstall watcher using the principles described above. To see it, connect to the device and open adb shell. Then call ps to see list of processes. You will see two processes similar to following
u0_a109 315 ... mobi.mgeek.TunnyBrowser
u0_a109 371 ... /data/data/mobi.mgeek.TunnyBrowser/files/watch_server
As you can see it starts a watch_server native program, which is a part of its apk-file. Now open App info page of Dolphin Browser and press "Force Stop". Switch back to terminal and call ps again. You will see there is no mobi.mgeek.TunnyBrowser process anymore, but watch_server still runs.
By the way this approach will only work, if watcher server runs all the time. To
make sure it is always up, both apps require "run at startup"
permission, where they start their watchers.
Now, when you uninstall the app, Android stops all processes belonging to this application. Watcher receives termination signal and opens browser with predefined URL and then shuts down.
I might look a bit different in some details, but the main concept behind this hack must be as described.
There could be a tricky thing like that application is also having watcher service.
You can check the permission used by that app may contain INSTALL and UNINSTALL permissions.
HOW IT WORKS:
instead of single app that may have 2 app bundle.
as and when you install it, this app is also installing some service that is watching your app status
When you try to uninstall that app the system broadcast is called which is handled by that service and will check that if your package is exist in installed application or not.
as soon as this service finds that your package is not in the list it calls an intent with action view with the web url to open the brawser.

Calling a method in the system process without a qualified user error

I'm using the notification listener service in android 4.4 and I'm coming across an error that causes my app to stop getting notifications posted.
It's fairly random, but when it happens I'm seeing:
12-31 01:40:44.080 21680-21680/? W/ContextImpl﹕ Calling a method in the system process without a qualified user:
android.app.ContextImpl.sendOrderedBroadcast:1192
android.app.ContextImpl.sendOrderedBroadcast:1183
android.content.ContextWrapper.sendOrderedBroadcast:390
com.android.settings.applications.ProcessStatsDetail.checkForceStop:314
com.android.settings.applications.ProcessStatsDetail.onResume:108
in the logs.
I can even recreate this by just entering the "process stats" section of the developer tools on the phone. As soon as I select an app that uses the notification listener the puts up this warning and unbinds and destroys the service and when it recreates is it doesn't always pick up on events again.
I'm not sure why this would happen, can anyone shed any light?
1) the NotificationListenerService is started by the system and the code for sendOrderedBroadcast throws a warning if it was called by the system process (here)
2) That is just a warning though. There is probably a crash somewhere else in your code. When a NotificationListenerService crashes, Android doesn't restart it unless you toggle the permission or restart your phone. Look for another crash and try either of those methods to restart it if it has. Starting it yourself will not work.
Check your AIDL Package name, for me the Package name in client and server were mismatched. Hope it help somebody

Categories

Resources