【问题标题】:Debug app that runs at boot调试在启动时运行的应用程序
【发布时间】:2018-05-25 08:54:50
【问题描述】:

我正在使用 Android Studio。

我为接收 android.intent.action.BOOT_COMPLETED 的应用添加了一个 BroadcastReceiver,接收器只是显示一个 Toast 进行测试。问题是我在 Android 启动后收到“应用程序已停止”消息。

我的第一个问题是:有没有办法在启动时调试它并自己查看问题出在哪里?因为我在 Android Studio 中看不到任何涉及该问题的日志。

我的第二个问题与问题本身有关。这是代码: XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myapp">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".AutoStartReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

Java:广播接收器

public class AutoStartReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "loaded", Toast.LENGTH_LONG).show();
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

第三个问题是:在receiver中做一些繁重的工作(读取一些文件并设置AlarmManager)而不是创建一个服务是否可以?因为从 API 26 开始,Android 对服务施加了很多限制。

谢谢

【问题讨论】:

    标签: java android broadcastreceiver boot


    【解决方案1】:

    对于调试部分,我使用了这个(在终端中,当模拟器打开时):

    adb shell am set-debug-app -w --persistent <your.app.package> 
    

    开始调试,然后在应用程序在模拟器上提示时单击将调试器附加到 android 进程

    要禁用此功能:

    adb shell am clear-debug-app <your.app.package>
    

    在这里找到答案:https://medium.com/@elye.project/debug-app-deeplink-at-launch-time-bdb2cf04a9e6

    【讨论】:

      【解决方案2】:

      启动时启动的应用程序可以在您的设备获得调试连接的第二秒进行调试,这通常发生在应用程序本身启动之前。只需打开 logcat 并观察设备和应用程序在重启后是否弹出。请注意,这假设应用程序是可调试的。不是的应用程序根本不会显示任何日志。

      由于这段代码的明显原因,您会收到 MyApp has unfortunately stopped 消息:

      public class AutoStartReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              Toast.makeText(context, "loaded", Toast.LENGTH_LONG).show();
              throw new UnsupportedOperationException("Not yet implemented");
          }
      }
      

      你抛出一个异常,意味着它会停止。

      至于你在服务中做什么,只要它使用合理数量的 RAM 和处理器(极重的服务更有可能被杀死以节省电池和内存)你很好

      【讨论】:

      • 在大多数情况下,就是这样。但是在Android启动时我无法获取应用程序的日志,日志为空
      猜你喜欢
      • 2015-05-13
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多