【问题标题】:Boot completed not working启动完成不工作
【发布时间】:2018-12-17 07:32:37
【问题描述】:

我正在开发橙色 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();

    }

}

【问题讨论】:

  • 使用清单发布广播接收器和清单部分。
  • 附加清单和接收器
  • 我们从接收器执行代码的时间非常少。尝试删除 showNotification 并尝试 startActivity()。

标签: android orange-pi


【解决方案1】:

如果您想获得更多时间,您可以在广播接收器中执行非常少的时间,您可以像这样使用 goAsync()

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

    //some little long running task 

    pendingResult.finish();
}

查看this官方文档了解更多详情。

在你的情况下它看起来像

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

【讨论】:

  • public void onReceive(Context context, Intent intent) { try { final PendingResult pendingResult = goAsync();线程.sleep(1000);未决结果.finish(); Intent activityIntent = new Intent(context, MainService.class); activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startService(activityIntent); } catch (Exception ex) { Log.e("BC", ex.getMessage()); } }
  • Log info CHECK IS Need to start app [background] com.example.ggt.helloserial:com.example.ggt.helloserial 用于广播 BroadcastRecord{42153558 u0 android.intent.action.BOOT_COMPLETED} 跳过交付订购的 [背景] BroadcastRecord{42153558 u0 android.intent.action.BOOT_COMPLETED} 出于任何原因:com.example.ggt.helloserial 计划广播 [背景]:current=false 收到 BROADCAST_INTENT_MSG
  • 还是响应 CHECK IS Need to start app [background] com.example.ggt.helloserial:com.example.ggt.helloserial 用于广播 BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED}出于任何原因跳过订购的 [背景] BroadcastRecord{421b61c8 u0 android.intent.action.BOOT_COMPLETED} 的交付:com.example.ggt.helloserial 调度广播 [背景]:current=false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
相关资源
最近更新 更多