Android广播接收器在接收时未运行服务\u启动\u已完成

Android广播接收器在接收时未运行服务\u启动\u已完成,android,service,broadcastreceiver,Android,Service,Broadcastreceiver,我正在尝试创建一个使用计步器的Android应用程序。当前在用户重新启动/关闭手机时无法启动服务。我使用的是5.1,并且已经在模拟器上进行了测试。我不确定它是否工作,因为日志没有出现在控制台中 非常感谢您的帮助 应用程序清单类: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="app.apphub

我正在尝试创建一个使用计步器的Android应用程序。当前在用户重新启动/关闭手机时无法启动服务。我使用的是5.1,并且已经在模拟器上进行了测试。我不确定它是否工作,因为日志没有出现在控制台中

非常感谢您的帮助

应用程序清单类:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="app.apphub.devon.walkingquest">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".WalkingQuestSplashScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".BootReceiver" android:exported="true" android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filtering>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filtering>

    </receiver>
    <service android:name=".StepCounterSensorRegister" android:enabled="true" android:exported="true"/>
</application>

</manifest>
StepCountersensor注册器类别:

public class StepCounterSensorRegister extends Service implements SensorEventListener {

SensorManager sensorManager;
Sensor sensor;
long globalSteps;
boolean flag = false;


@Override
public void onCreate(){

}

public int onStartCommand(Intent intent, int flags, int startId) {
    //Must run in background thread
    Log.i("START","STEP_COUNTER_STARTED");
    registerSensor(sensorManager.SENSOR_DELAY_UI);
    return START_STICKY;
}

public void onSensorChanged(SensorEvent event) {

    if(!flag){

    }
    if(event.sensor.getType() == Sensor.TYPE_STEP_COUNTER){
        Log.i("STEPS DETECTED",""+globalSteps);
        globalSteps++;
    }
}

public boolean registerSensor(int accuracy) {
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    try {
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    }catch (Exception e){
        Log.i("ERROR", e.getMessage());
    }

    if(sensor == null) {
        Log.i("FAILED","Sensor returned null");
        return false;
    }
    else {
        Log.i("SUCCESS", "Successfully registered sensor");
        sensorManager.registerListener(this, sensor, accuracy);
        return true;
    }

}

我看到的两件事是,你在清单中声明了一些错误:

 <receiver android:name=".BootReceiver" android:exported="true" android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filtering>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filtering>

    </receiver>

  • intent-filter
    不存在,它是
    intent-filter
  • 权限
    android:permission=“android.permission.RECEIVE\u BOOT\u COMPLETED”
    放错了地方,不要放在receiver标签内。您已经在上面定义了它

  • 请确保在Logcat中设置了正确的过滤器,您使用了两个不同的标签“广播”和“失败”,可能这也会出错。

    非常感谢!删除额外的权限,并修复意图过滤器对我很有效。
     <receiver android:name=".BootReceiver" android:exported="true" android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filtering>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filtering>
    
        </receiver>