我想在我的android項目中創建一個服務,但該服務似乎並沒有開始。Android的java服務不工作
package serviceexample.javatechig.com.serviceexample;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class HelloService extends Service {
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
}
}
的manifest.xml:
<service android:name="serviceexample.javatechig.HelloService" android:exported="false"/>
和主要活動:
package serviceexample.javatechig.com.serviceexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
findViewById(R.id.start_service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this, HelloService.class);
startService(intent);
}
});
findViewById(R.id.stop_Service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this, HelloService.class);
stopService(intent);
}
});
}
}
沒有錯誤,但是當我按下按鈕,該服務不會得到started.onCreate和onStartCommand事件不提高。
可能重複[爲什麼這個簡單服務不啓動?] (http://stackoverflow.com/questions/4759676/why-is-this-simple-service-not-starting) – 0X0nosugar
看看第二個答案:「非常重要:正確地寫下名字空間......」 – 0X0nosugar
我的'在java.do中新建一個你知道什麼是正確的名字? –