我正在尝试创建一个使用计步器的Android应用。当用户重新启动/打开和关闭手机时,目前无法启动服务。我使用5.1,测试已在模拟器上完成。我不确定它是否正常工作,因为日志没有出现在控制台中。
非常感谢任何帮助!
App Manifest Class:
<?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>
启动接收器类: public class BootReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i("BROADCAST", "WALKING_QUEST_BOOT");
Intent _intent = new Intent("app.apphub.devon.walkingquest.StepCounterSensorRegister");
intent.setClass(context, StepCounterSensorRegister.class);
context.startService(_intent);
//context.startService(_intent);
//Toast.makeText(context,"Airplane mode on",Toast.LENGTH_LONG).show();
}
else {
Log.i("FAIL","BROADCAST FAILED");
}
}
StepCounterSensorRegister Class:
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;
}
}
答案 0 :(得分:2)
我看到的两件事是您在清单中声明了错误:
<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-filtering
不存在,它是intent-filter
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
在这里放错了地方,不要把它放在接收器标签内。你已经在上面定义了它。确保在Logcat中设置了正确的过滤器,你使用了两个不同的标签“BROADCAST”和“FAIL”,也许这也是错误的。