2017-02-27 130 views
1

我正尝试创建一个使用计步器的Android应用程序。目前无法在用户重新启动/关闭手机时启动服务。我正在使用5.1,并且已经在模拟器上完成了测试。由于日志没有出现在控制台中,我不确定它是否正常工作。Android广播接收器未运行RECEIVE_BOOT_COMPLETED上的服务

任何帮助非常感谢!

应用清单类:

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

启动接收机类: 公共类BootReceiver扩展广播接收器{

@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类:

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; 
    } 

} 

回答

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> 
  1. intent-filtering doesn't存在,它是intent-filter
  2. 权限android:permission="android.permission.RECEIVE_BOOT_COMPLETED"这里放错了地方,鸵鸟政策把它接收标签内。您已经在上面定义了它。

并且确保您在Logcat中设置了正确的过滤器,您使用了两个不同的标签“BROADCAST”和“FAIL”,也许这也出错了。

+0

非常感谢!删除额外的权限,并修复intent-filter为我工作。 –