【问题标题】:Start service in Android Activity and nothing happened在 Android Activity 中启动服务并没有发生任何事情
【发布时间】:2018-01-21 09:16:25
【问题描述】:

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 没有被调用,没有错误日志

一些日志

  • 08-13 05:51:34.935 2885-2885/? W/System:ClassLoader 引用了未知路径: /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/art:在 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 中的 package-private 方法
  • 08-13 05:51:35.183 2885-2885/? I/DemoLog: beforetest startService
  • 08-13 05:51:35.187 2885-2885/? I/DemoLog:后测启动服务
  • 08-13 05:51:35.490 2885-2937/com.mxyue.www.testdemo I/OpenGLRenderer:初始化 EGL,1.4 版
  • 08-13 05:51:35.548 2885-2937/com.mxyue.www.testdemo W/EGL_emulation:eglSurfaceAttrib 未实现

我发现像我这样的其他人的代码工作正常。为什么我的代码不起作用。

这个效果是我吗?

ClassLoader 引用了未知路径:

【问题讨论】:

  • 服务的类与您在清单中指定的包不同。
  • 感谢@orip 我只是在这个问题中写了错误。我的代码是正确的,但不起作用。
  • 尝试显式设置包:intent.setPackage(this.getPackageName());
  • 我添加了 'intent1.setPackage(this.getPackageName());'在'Intent intent1 = new Intent(MainActivity.this, TestService.class);'下什么也没发生。

标签: android android-studio service


【解决方案1】:

TestService 类的包是package com.mxyue.www.testdemo。在清单中也使用相同的:&lt;service android:name="com.mxyue.www.testdemo.TestService"&gt;&lt;/service&gt;

【讨论】:

  • 对不起!我已经试过很多次了。当我写问题时,我在清单中丢失了“testdemo”。结果没有改变,仍然没有启动服务。我编辑了这个错误的问题。
猜你喜欢
  • 2012-06-10
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多