Загрузка завершена, не работает

Я работаю над оранжевым пи 2g IoT и BroadcastReceiver не удается запустить действие или службу при завершении загрузки. Когда приложение работает, оно может перехватить загрузку завершено, но оно не работает, так как не может перехватить трансляции.

Приложенный журнал:

07-09 22:49:26.840 509-523/system_process V/BroadcastQueue: Received BROADCAST_INTENT_MSG
processNextBroadcast [background]: 0 broadcasts, 1 ordered broadcasts
processNextBroadcast : br=BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Processing ordered broadcast [background] BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Submitting BROADCAST_TIMEOUT_MSG [background] for BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED} at 328627
CHECK IS Need to start app [background] com.example.b_oyu.startuptest:com.example.b_oyu.startuptest for broadcast BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}
Skipping delivery of ordered [background] BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED} for whatever reason about :com.example.b_oyu.startuptest
Schedule broadcasts [background]: current=false
Received BROADCAST_INTENT_MSG
processNextBroadcast [background]: 0 broadcasts, 1 ordered broadcasts
processNextBroadcast : br=BroadcastRecord{42b4d998 u-1 android.intent.action.BOOT_COMPLETED}

а также

 CHECK IS Need to start app [background] com.example.ggt.helloserial:com.example.ggt.helloserial for broadcast BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED}
    Skipping delivery of ordered [background] BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED} for whatever reason about :com.example.ggt.helloserial
    Schedule broadcasts [background]: current=false
    Received BROADCAST_INTENT_MSG

Manifiest

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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="com.example.ggt.helloserial.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.example.ggt.helloserial.BootCompleted"
            android:label="BootCompleted"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.REBOOT"/>
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.HOME"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <service android:name=".MainService"></service>
    </application>
</manifest>

BroadcastReceiver

public class BootCompleted extends BroadcastReceiver {

    CustomNotification NotMan= new CustomNotification();
    public BootCompleted() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        final PendingResult pendingResult = goAsync();
        try
        {
//            Thread.sleep(1000);

            Intent activityIntent = new Intent(context, MainService.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Log.e("BC", "From BootCompleted");
            NotMan.ShowNotification(context,"From BootCompleted:" + intent.getAction(),0 );
            Log.e("BC", "From BootCompleted");
            context.startService(activityIntent);

        }
        catch (Exception ex)
        {
            Log.e("BootCompletedError", ex.getMessage());
        }
        pendingResult.finish();

    }

}

1 ответ

У вас очень мало времени выполнения в приемниках Broadcast, если вы хотите получить больше времени, вы можете использовать goAsync() следующим образом.

@Override
public void onReceive(final Context context, final Intent intent) {
    final PendingResult pendingResult = goAsync();

    //some little long running task 

    pendingResult.finish();
}

Проверьте этот официальный документ для более подробной информации.

в вашем случае это будет выглядеть

@Override
    public void onReceive(final Context context, final Intent intent) {
        final PendingResult pendingResult = goAsync();

       try{
                Intent activityIntent = new Intent(context, MainActivity.class);
                activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Log.e("BC", "From BootCompled");
            NotMan.ShowNotification(context,"From BootCompled:" + intent.getAction(),0 );
                context.startActivity(activityIntent);
            }
            catch (Exception ex){Log.e("BC", ex.getMessage());}

        pendingResult.finish();
    }
Другие вопросы по тегам