Can my Android app detect if it was launched from Eclipse?

Is there a way for an Android activity to detect how it was launched? Specifically, I would like to detect if my app was started from Eclipse, versus if it was started normally (eg, from the home screen or installed application list).

This is a proxy for knowing if crash reports should be uploaded or not. I'm assuming if Eclipse launched the app, then I don't need to uploaded crashes, because I'm already debugging the app, but if the same build of the app is started "normally", I'd like to upload any exceptions.

This is just to make my personal debugging and development easier, so unshippable hacks or tweaks to Eclipse are useful to me.


This method appears to do what you require:

if(!android.os.Debug.isDebuggerConnected()) {
    // Send report...
}

This should tell you if you're currently attached to a debugger. If launched from the app drawer or a homescreen shortcut, you won't be attached (unless you already have a running instance that is attached) and this method will return false - in this case you can upload your crash information.


你可以使用if (BuildConfig.DEBUG)来告诉一个发布版本的调试版本。


You (I?) can add a new activity to the app, and have Eclipse launch that. The new activity will set a flag that otherwise doesn't get set, and otherwise be the same. Regular use of the app will come through the default activity, and so the flag will be defaulted to unset.

Add a new DebugActivity that extends existing activity (call it MainActivity ):

public class DebugActivity extends MainActivity {
    public DebugActivity() {
        super(true); // running from Eclipse
    }
}

Add a new boolean constructor and no-arg constructor to MainActivity to support the new subclass:

private final boolean viaEclipse;

public MainActivity() {
    this(false);
}

public MainActivity(boolean viaEclipse) {
    this.viaEclipse = viaEclipse;
}

Duplicate the <activity> information in the app manifest for .MainActivity , but change the duplicate to reference the new .DebugActivity . Remove the <category> tag (the "LAUNCHER" one) from the .DebugActivity <intent-filter> to prevent it from showing up in the Android UI.

In Eclipse, choose Run -> Run Configurations ... . Select the existing run configuration for the app. Click the icon that "Duplicates the currently selected Launch Configuration", and on the Android tab in the duplicate change the Launch Action from "Launch Default Activity" to "Launch: " and select the new .DebugActivity .

Downsides

  • Eclipse "debug" runs are not identical to a "real" run, but the difference is pretty small (basically just the launch class name and an extra entry on the stack). There is no duplicated code on the Java side, at least.

  • I duplicated most of the content of my <activity> in the manifest, which is a little fragile but not too bad.

  • Still would be nice to have something simpler than this.

    链接地址: http://www.djcxy.com/p/72626.html

    上一篇: 在使用phonegap运行android命令时创建android子项目时出错

    下一篇: 我的Android应用可以检测它是否从Eclipse启动?