-1

我正在开发橙色 pi 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

清单

<?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>

广播接收器

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();

    }

}
4

1 回答 1

0

如果您想获得更多时间,您可以在广播接收器中执行非常少的时间,您可以像这样使用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();
    }
于 2018-07-16T10:57:07.810 回答