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

时间:2017-12-11 19:01:35

标签: java android broadcastreceiver boot

我正在使用Android Studio。

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

我的第一个问题是:无论如何都要在启动时调试它,看看问题出在哪里?因为我在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:BroadcastReceiver

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");
    }
}

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

谢谢

2 个答案:

答案 0 :(得分:3)

启动时启动的应用程序可以在您的设备获得调试连接的第二个调试,这通常发生在应用程序本身启动之前。只需打开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和处理器中使用(非常重的服务更可能被杀死以节省电池和内存)你很高兴

答案 1 :(得分:0)

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

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

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

要禁用此功能:

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

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