【问题标题】:BOOT_COMPLETED isn't received by my physical phone Asus我的实体手机华硕没有收到 BOOT_COMPLETED
【发布时间】:2018-07-15 13:05:14
【问题描述】:

我测试了一个在模拟器中运行JobIntentServiceBroadcastReceiver,它运行成功。

但是当我测试我的物理手机华硕 ZE551ML 的代码时它失败了。

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<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=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
  </receiver>

  <service android:name=".MyJobIntentService"
        android:permission="android.permission.BIND_JOB_SERVICE"

        android:exported="false"/>
</application>

BroadcastReceiver 类:

package com.packt.backgroundservicedemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Write Code..

        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MyJobIntentService.class);
            i.putExtra("sleepTime", 12);
            MyJobIntentService.enqueueWork(context,i);
//            context.startService(i);

        }


    }
}

JobIntentService 类:

package com.packt.backgroundservicedemo;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import android.widget.Toast;


public class MyJobIntentService extends JobIntentService {

    static final int JOB_ID = 15;


    static void enqueueWork(Context context,Intent intent){
        enqueueWork(context,MyJobIntentService.class,JOB_ID,intent);
    }

    private static final String TAG = MyJobIntentService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Task Execution Started",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {

        // Write code here..
        Log.i(TAG,"onHandleWork(), Thread name:" + Thread.currentThread().getName());

        int duration = intent.getIntExtra("sleepTime",-1);
        int ctr = 1;
        //Dummy long operation
        while (ctr<=duration){
            Log.i(TAG,"Time elapsed " + ctr + " secs");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ctr++;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"Task Execution Finished",Toast.LENGTH_SHORT).show();



    }
}

重复:代码在模拟器设备、Pre Oreo 和 Oreo 设备上运行良好。

日志不包含任何与我的应用程序相关的boot_completed reveiced

谢谢。

【问题讨论】:

    标签: android broadcastreceiver bootcompleted jobintentservice asus


    【解决方案1】:

    我敢打赌 - 你的模拟器没有密码锁,而你的真实设备有。 BOOT_COMPLETED 仅在使用密码解锁后广播,您必须等待其他 BroadcastReceivers 接收此事件(注册此事件的其他应用程序),具体取决于您在队列中的位置系统。

    【讨论】:

    • 感谢 Arseny,但在解锁设备后应用程序也不会收到 broadcast receiver。我还检查了日志,看到一些应用程序接收boot_completed 接收器。
    • 我可以做任何特定的代码或操作来支持华硕手机吗?我觉得问题来自于它。
    • 不具体了解华硕。也许检查他们是否有自己的权限管理器(就像小米一样),这需要使用他们特定的 API。
    【解决方案2】:

    有一个自动启动管理器应用程序拒绝我的应用程序在启动时运行。

    现在我的应用程序在开始时运行良好。

    感谢@Arseny Levin 的提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2011-04-25
      • 2017-07-30
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多