未找到Android服务(联系更改检测)

时间:2011-09-29 08:23:41

标签: java android android-manifest

我正在尝试让我的Android应用响应本地地址簿中的更改,并根据需要与其同步。

据我所知,为了做到这一点,我需要创建一个在后台运行的服务。我做到了。

但是它不起作用...当我的应用程序运行并且我尝试启动服务时,它失败了:

 [exec] [DEBUG] I/ActivityManager(   60): Starting activity: Intent { cmp=ti.test/my.Activity (has extras) }
 [exec] [DEBUG] E/aas     ( 1812): (main) [490639,495460] people uri:content://contacts/people
 [exec] [DEBUG] W/ActivityManager(   60): Unable to start service Intent { cmp=ti.test/.MyService }: not found

这就是我尝试运行它的方式:

startService(new Intent(activity, MyService.class));

这是我的manifest.xml fie的一部分:

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest android:versionCode="1" android:versionName="1">
        <uses-sdk android:minSdkVersion="4"/>
        <supports-screens android:anyDensity="true"
            android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
        <application>
            <service android:name="ti.test.MyService"/>
        </application>

    </manifest>
</android>

这是服务类:

package ti.test;

import android.app.Service;
import android.content.Intent;

import android.os.IBinder;
import android.util.Log;

import android.database.ContentObserver;
import android.provider.Contacts.People;

public class MyService extends Service 
{
    private static final String TAG = "MyService";



    private class MyContentObserver extends ContentObserver {

        public MyContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.e ("DatabaseTable" , "****************************** contact database change detected *************************************");
        }

        @Override
        public boolean deliverSelfNotifications() {
            return true;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d("MyService", "starting........");
        MyContentObserver contentObserver = new MyContentObserver();
        getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);


    }




    @Override
    public void onDestroy() {
        Log.d("MyService", "stopping........");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }


    @Override
    public void onStart(Intent intent, int startid) {
        Log.d("MyService", "onStart........");

    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

你不应该在服务中创建类。 你应该定义一个contectResolver类并在一个活动中或在你想要使用它的地方注册它。