MainActivity.java在Android的活動開始服務,什麼都沒有發生
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不叫,不會有錯誤日誌
一些日誌
- 5月8日至13日:51:34.935 2885年至2885年/? W/System:ClassLoader reference unknown path: /data/app/com.mxyue.www.testdemo-1/lib/x86
- 08-13 05:51:34.937 2885-2885 /? I/InstantRun:啓動即時運行服務器:是主進程
- 08-13 05:51:34.982 2885-2885 /? W /藝術:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)將有錯誤地重寫了android.graphics.drawable.Drawable中的包私有方法
- 08-13 05:51:35.183 2885-2885 /? I/DemoLog:beforetest startService
- 08-13 05:51:35.187 2885-2885 /? I/DemoLog:aftertest startService
- 5月8日至13日:51:35.490 2885年至2937年/ com.mxyue.www.testdemo I/OpenGLRenderer:初始化EGL,1.4版本
- 5月8日至13日:51:35.548 2885- 2937/com.mxyue.www.testdemo W/EGL_emulation:eglSurfaceAttrib not implemented
我發現像我這樣的其他人代碼工作正常。爲什麼我的代碼不起作用。
這是我的作用嗎?
類加載器引用未知路徑:
服務的類與您在清單中指定的不同。 – orip
謝謝@orip我只是寫在這個問題上的錯誤。我的代碼是正確的,但沒有工作。 –
嘗試顯式設置包:'intent.setPackage(this.getPackageName());' – Vasiliy