How to debug with Android App Widget?

前端 未结 4 1048
有刺的猬
有刺的猬 2020-12-20 12:35

For normal Activity, I can set some breakpoints and click F11 in Eclipse to debug. However, it is not working when I develop app widget. So, how can I debug?

相关标签:
4条回答
  • 2020-12-20 13:04

    Android Studio

    I did not find a "structured" way to explicitly debug an App Widget.

    My workaround:

    Make sure you have an Activity in your project - usually you will have at least a Setting Activity, but if not, just make a dummy Activity that doesn't have to do anything.

    In your Manifest, add this Activity and mark it as the launching activity:

    <activity android:name=".activity.SettingActivity"
        android:label="@string/setting_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    

    now you should place your breakpoint wherever you like to debug, and launch your application using the button. Now the code of the whole application is under the debug session and the debugger will stop at any breakpoint, including the widget.

    Note: before production, remove the launching intent filter from your manifest, if you don't mean for this Activity to be launched directly from the device launchpad

            <activity android:name=".activity.**SettingActivity**"
            android:label="@string/setting_name">
            <!--<intent-filter>-->
                <!--<action android:name="android.intent.action.MAIN"/>-->
                <!--<category android:name="android.intent.category.LAUNCHER"/>-->
            <!--</intent-filter>-->
        </activity>
    
    0 讨论(0)
  • 2020-12-20 13:09

    Android Studio

    It is really simple. Just set up your debug points within your class that extends AppWidgetProvider.

    From here...

    1. Start up your emulator OR Hook up your phone via USB cable
    2. Select the Debug icon like normally.
    3. Wait for your app to start
    4. Go to your home screen
    5. Add your Widget OR go to the screen with your Widget already on it

    From this point, if you have a breakpoint within the onUpdate(...) method, then your debugging of the widget will start

    Eclipse

    StarsSky's answer

    0 讨论(0)
  • 2020-12-20 13:13

    Here you can find a good answer:

    All you need to debug your widget code is almost same as what you do for normal applications. Just follow the below steps:

    1. Press “debug” on the eclipse menu (or “run” it doesn’t seem to matter)

    2. Once the widget apk is sync’ed and installed onto your emulator/device, switch your eclipse workspace to DDMS mode. You can either do this by pressing the “DDMS” labeled button on your top right corner or if you can’t find it, then do it by going to “Window->Open Perspective->DDMS”.

    3. Select the process name of your widget from the list of processes shown. By default, this list appears at top left of DDMS window. (See screenshot below). If you can’t see your widget’s process name in the list, it is possible that the widget is not added to the home screen yet. So, do so.

    4. Press the green debug button above the process list (See screenshot below)

    5. And that’s it. Now, if you had put a breakpoint in the code, do something that executes that piece of the code.

    enter image description here

    0 讨论(0)
  • 2020-12-20 13:14

    I assume with "App Widget" you mean the widgets users can add to their home screens by long pressing on the background wallpaper?

    If your AndroidManifest.xml file is set up properly you are able to debug these widgets just like any other Android application.

    However, note that you need to add your widget to the home screen first.

    Once you've done that, you should see your widget process being listed under the DDMS mode perspective in Eclipse. You can attach the debugger and debug your code.

    0 讨论(0)
提交回复
热议问题