调试启动时运行的应用程序

hid*_*s02 2 java boot android broadcastreceiver

我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

hid*_*s02 5

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

adb shell am set-debug-app -w --persistent <your.app.package> 
Run Code Online (Sandbox Code Playgroud)

开始调试,然后当应用程序在模拟器上提示时单击“将调试器附加到 Android 进程” 。在此输入图像描述

要禁用此功能:

adb shell am clear-debug-app <your.app.package>
Run Code Online (Sandbox Code Playgroud)

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