我是新来的,我有一个关于android服务的问题。不知什么原因,我的服务没有开始。在日志中没有任何错误。我用过

startService(new Intent(getApplicationContext(),MyResultsService.class));

调用主活动中的服务(myResultsService.Class)。
另外,这是myresultsservice.class:
package com.example.myapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyResultsService extends Service{
    public UpdateMyResults updater;
    public boolean stopThread = false;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d(getPackageName(), "Created MyResultsService");
    }

    @Override
    public synchronized int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.d(getPackageName(), "Starting...");
        if (!updater.isAlive()){
            updater = new UpdateMyResults();
            Log.d(getPackageName(), "New updater thread...");
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public synchronized void onDestroy() {
        // TODO Auto-generated method stub
        stopThread = true;
        Log.d(getPackageName(), "Destroying MyResultsService");
        try {
            updater.join();
            Log.d(getPackageName(), "Closed updater thread");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private class UpdateMyResults extends Thread{

        static final long DELAY = 30000;
        @Override
        public void run() {
            while (!stopThread){
                try {
                    //Do stuff and pause
                    Log.d(getName(), "Running");
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    // Interrupt
                    e.printStackTrace();
                }
            }//end while
        }//end run
    }//end UpdateMyResults
}

我的XML清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />

    <application
        android:label="MyApp"
        android:icon="@drawable/ic_launcher"
        android:theme="@android:style/Theme.Holo.Light">

        <activity
            android:name=".MainActivity"
            android:label="My App">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" />

    </application>

</manifest>

任何帮助都将不胜感激,谢谢!

最佳答案

<service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" />中的名称必须与您的类完全匹配。尽量不要全部使用大写字母。

10-07 19:26
查看更多