Android service not found (contact change detection)
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........");
}
@Over开发者_高级运维ride
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!
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.
精彩评论