MainActivity.java
package com.mxyue.www.testdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("DemoLog", "beforetest startService");
Intent intent1 = new Intent(MainActivity.this, TestService.class);
startService(intent1);
//stop Service
Intent intent4 = new Intent(this,TestService.class);
stopService(intent4);
//restart Service
Intent intent5 = new Intent(this,TestService.class);
startService(intent5);
Log.i("DemoLog", "aftertest startService");
}
}
TestService.java
package com.mxyue.www.testdemo;
/**
* Created by mxyue on 2017/8/13.
*/
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service{
@Override
public void onCreate(){
Log.i("DemoLog","TestService -> onCreate, Thread ID:"+Thread.currentThread().getId());
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
Log.i("DemoLog","TestService -> onStartCommand, startId: "+startId+",Thread ID: "+Thread.currentThread().getId());
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent){
Log.i("DemoLog","TestService -> onBind, Thread ID: "+Thread.currentThread().getId());
return null;
}
@Override
public void onDestroy(){
Log.i("DemoLog","TestService -> onDestroy, Thread ID: "+Thread.currentThread().getId());
super.onDestroy();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mxyue.www.testdemo">
<application>
...
</application>
<service android:name="com.mxyue.www.testdemo.TestService"></service>
</manifest>
未调用onStartCommand,没有错误日志
一些日志
我发现像我这样的其他代码工作正常。为什么我的代码不起作用。
这是影响我吗?
ClassLoader引用了未知路径:
答案 0 :(得分:0)
班级TestService
的包是package com.mxyue.www.testdemo
。也可以在清单中使用相同的内容:<service android:name="com.mxyue.www.testdemo.TestService"></service>