2017-04-05 93 views
0

我的服務類沒有調用,沒有錯誤,崩潰報告或異常!調用服務之前和之後的行正在工作,我也檢查了使用isMyServiceRunning方法,並且它不工作..服務類不工作

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent i = new Intent(this, MyService.class); 
    startService(i); 
    Toast.makeText(this,"to check if this is working",Toast.LENGTH_LONG).show(); 
    } 

} 

服務類:

public class MyService extends Service implements SensorEventListener { 

private SensorManager mSensorManager; 
private Sensor mSensor; 

double ax, ay, az; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 


    if (mSensor != null) { 
     mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); 
    } else { 

     stopSelf(); 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
public void onSensorChanged(SensorEvent event) { 

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 

     ax = event.values[0]; 
     bx = event.values[1]; 
     cx = event.values[2]; 
    } 
} 

@Override 
public void onAccuracyChanged(Sensor arg0, int arg1) { 
} 
} 

清單文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.star.sproject"> 

<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" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".MyService" 
     android:enabled="false" 
     android:exported="false"></service> 
</application> 

請幫忙!

+0

啓用= 「假」 是指服務未啓用 –

回答

0

做這個

<service 
     android:name=".MyService" 
     android:enabled="true" 
     android:exported="true"> 
    </service> 
+0

非常感謝你! – mystic

+0

您的歡迎。 :)如果解決了您的問題,請接受答案。 – Simo