未找到 Android 服务(接触变化检测)

发布于 2024-12-07 01:03:18 字数 2914 浏览 0 评论 0原文

我正在尝试让我的 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的一部分,即:

<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........");

    }
}

谢谢!

I am trying to get my Android app to respond to changes in the local address book and sync with it on a need basis.

I understand that in order to do that, I need to create a service to run in the background. I did.

But it isn't working... when my app runs and I try to initiate the service, it fails with :

 [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

this is how I try to run it:

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

this is part of my 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>

this is the service class:

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........");

    }
}

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-12-14 01:03:18

你不应该在服务中创建类。
您应该定义一个 contectResolver 类并将其注册到活动中或您想要使用它的位置。

you should not create class in service.
you should define a contectResolver class and register it in an activity or where you want to use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文