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. :)
Related
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?
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.
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.
This code will run an app automatically after booting the system, but the app will close after pressing the back button.
If the app is run normally by clicking it's icon. It will continuously run even after pressing the back button or running other apps.
public class AutoBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
My question is, how to make this auto run code to continuously run even after pressing the back button or running other apps?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
UDPATE
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
As apps run in the background anyway, I'm assuming what your really asking is how do you make apps do stuff in the background. The solution below will make your app do stuff in the background after opening the app and after the system has rebooted.
Below, I've added a link to a fully working example (in the form of an Android Studio Project).
This subject seems to be out of the scope of the Android docs, and there doesn't seem to be any one comprehensive doc on this. The information is spread across a few docs.
The following docs tell you indirectly how to do this:
https://developer.android.com/reference/android/app/Service.html
https://developer.android.com/reference/android/content/BroadcastReceiver.html
https://developer.android.com/guide/components/bound-services.html
In the interests of getting your usage requirements correct, the important part of this above doc to read carefully is: #Binder, #Messenger and the components link below:
https://developer.android.com/guide/components/aidl.html
Here is the link to a fully working example (in Android Studio format):
https://developersfound.com/BackgroundServiceDemo.zip
This project will start an Activity which binds to a service; implementing the AIDL.
This project is also useful to re-factor for the purpose of IPC across different apps.
This project is also developed to start automatically when Android restarts (provided the app has been run at least one after installation and app is not installed on SD card).
When this app/project runs after reboot, it dynamically uses a transparent view to make it look like no app has started but the service of the associated app starts cleanly.
This code is written in such a way that it's very easy to tweak to simulate a scheduled service.
This project is developed in accordance to the above docs, and is subsequently a clean solution.
There is, however, a part of this project which is not clean: I have not found a way to start a service on reboot without using an Activity. If anyone reading this post has a clean way to do this, please post a comment.
Starting an Activity is not the right approach for this behavior. Instead have your BroadcastReceiver use an intent to start a Service which can continue to run as long as possible. (See http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)
See also Persistent service
I have written a service with a remote interface and installed it on my PC's Eclipse AVD. I have a client test harness which starts and invokes methods in the service. Initially I had the service installed by a control class and activity, which I have now removed, so that the manifest for the service looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.gridservice"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:debuggable="true">
<service
android:enabled="true"
android:debuggable="true"
android:name="OverlayService">
<intent-filter>
<action android:name="com.myname.OverlayService.SERVICE"/>
<action android:name="com.myname.gridservice.IRemoteInterface" />
</intent-filter>
</service>
</application>
</manifest>
so there's no activity tag.
When I launch it from the debug icon in Eclipse, the console tells me that it's installing the apk (which it is), but it does not appear as a debug thread and breakpoints aren't triggered, although the service's behaviour is OK as far as the client sees it. If I wrap the service tag in an activity tag which has an associated class and launch that, then I can debug it.
Is it possible to debug the service without wrapping it in an activity?
Here's what you can do in four steps:
First: In the first interesting method of your service (I used on create):
/* (non-Javadoc)
* #see android.app.Service#onCreate()
*/
#Override
public void onCreate() {
super.onCreate();
//whatever else you have to to here...
android.os.Debug.waitForDebugger(); // this line is key
}
Second: Set break points anywhere after the waitForDebugger command.
Third: Launch app via debug button in your IDE (Eclipse/Android Studio/...). (You should probably have removed the main launch activity from the manifest by now)
Last: Launch adb and run the command to start a service:
cd $PLATFORM_TOOLS
adb shell
am startservice -n com.google.android.apps.gtalkservice/com.google.android.gtalkservice.service.GTalkService
just make sure you don't forget this line of code in your code and release your apk. if you try running your app without the debugger the line below will get stuck.
android.os.Debug.waitForDebugger();
also you can use the following to determine if the debugger is connected:
android.os.Debug.isDebuggerConnected(); //Determine if a debugger is currently attached.
Edit 2018 button icon changed
This is pretty simple, you can connect to your application service. Assuming that running the debugger doesn't work, you can press the choose process option by pressing the debug with an arrow icon pictured above. Select your service and you should now be able to debug your service.
I think it should be done programmatically with android.os.Debug.waitForDebugger();
This works in Android Studio. There might be a similar way in Eclipse I suppose.
run your project in debug mode
attach debugger to your service process
Some of the answers correctly mention that you'd want to insert
android.os.Debug.waitForDebugger();
into the first interesting method of the service. However, it's not clear from those answers, that the Android Studio debugger will not start automatically when the service is started.
Instead, you also need to wait till the service has started, then press the button to attach to process (see screenshot for Android Studio 3.6.1 .. it is the 3rd button to the right from the debug button). You will be given a choice of processes to attach to, one of which would be the service's separate process. You can then select it to complete the process of attaching the debugger to the service.
Edit, Aug 2020: the button icon is the same in Android Studio 4.0