Android应用程序无法启动服务

rog*_*gcg 3 android android-service android-contentprovider

嘿,我正在尝试在我的Android应用程序上实现一项服务.服务必须执行活动的相同任务.IE,如果在CallLog.Calls内容提供程序上发生某些更改,则必须通知服务并将数据插入数据库,即使应用程序未运行,我的意思是,服务将在应用程序启动后运行,因此如果应用程序被杀死,服务将继续运行,直到操作系统停止它,对吧?

因此它将在后台运行,收集CallLog.Calls服务上更改的所有数据.但是,该服务没有运行.我在Activity的onCreate()方法中加注它.在Service中,我实现了一个ContentObserver类,它使用onChange()方法,以防CallLog.Calls内容提供程序中的某些更改.

我不知道的是为什么服务没有启动,以及为什么即使我在DDMS视角杀死应用程序它也不起作用.

这是代码.

该Activity名为RatedCalls.java

public class RatedCalls extends ListActivity {

private static final String LOG_TAG = "RATEDCALLSOBSERVER";
private Handler handler = new Handler();
private SQLiteDatabase db;
private CallDataHelper cdh;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);
private Integer contentProviderLastSize;
private Integer contentProviderCurrentSize;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    cdh = new CallDataHelper(this);
    db = openHelper.getWritableDatabase();

    startService(new Intent(this, RatedCallsService.class));
    registerContentObservers();

    Log.i("FILLLIST", "calling from onCreate()");

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    contentProviderLastSize = cursor.getCount();

}

class RatedCallsContentObserver extends ContentObserver {
    public RatedCallsContentObserver(Handler h) {
        super(h);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;

    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");
        super.onChange(selfChange);
        searchInsert();

    }
}

private void searchInsert() {

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    Log.i("FILLLIST", "Calling from searchInsert");

    startManagingCursor(cursor);
    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int numTypeId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;

    SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
    Date date = new Date();

    cursor.moveToFirst();           

            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String numType = cursor.getString(numTypeId);
            stopManagingCursor(cursor);
            ContentValues values = new ContentValues();

            values.put("contact_id", 1);
            values.put("contact_name", contactName);
            values.put("number_type", numType);
            values.put("contact_number", contactNumber);
            values.put("duration", duration);
            values.put("date", dateFormat.format(date));
            values.put("current_time", currTime);
            values.put("cont", 1);

            db.insert(CallDataHelper.TABLE_NAME, null, values);         
}
    public void registerContentObservers() {

    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));

}
Run Code Online (Sandbox Code Playgroud)

这就是名为RatedCallsService.java的服务

public class RatedCallsService extends Service {

private static final String TAG = "RatedCallsService";
private static final String LOG_TAG = "RatedCallsService";
private Handler handler = new Handler();
private SQLiteDatabase db;
private CallDataHelper cdh;
OpenHelper openHelper = new OpenHelper(RatedCallsService.this);

class RatedCallsContentObserver extends ContentObserver {
    public RatedCallsContentObserver(Handler h) {
        super(h);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;

    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");
        super.onChange(selfChange);
        searchInsert();

    }
}

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

@Override
public void onCreate() {

    Toast.makeText(this, "Rated Calls Service Created", Toast.LENGTH_LONG).show();
    Log.i(TAG, "onCreate");
    registerContentObservers();

}

@Override
public void onDestroy() {

    Toast.makeText(this, "Rated Calls Service Stopped", Toast.LENGTH_LONG).show();
    Log.i(TAG, "onDestroy");
    cdh = new CallDataHelper(this);
    db = openHelper.getWritableDatabase();

}

@Override
public void onStart(Intent intent, int startid) {

    Toast.makeText(this, "Rated Calls Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");      
    registerContentObservers();

}

private void searchInsert() {

    Cursor cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");

    Log.i("FILLLIST", "Calling from searchInsert");

    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int numTypeId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;

    SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
    Date date = new Date();

    if (cursor.moveToFirst()) {
        do {

            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String numType = cursor.getString(numTypeId);           
            ContentValues values = new ContentValues();

            values.put("contact_id", 1);
            values.put("contact_name", contactName);
            values.put("number_type", numType);
            values.put("contact_number", contactNumber);
            values.put("duration", duration);
            values.put("date", dateFormat.format(date));
            values.put("current_time", currTime);
            values.put("cont", 1);

            db.insert(CallDataHelper.TABLE_NAME, null, values);

        } while (cursor.moveToNext());
        cursor.close();
    }
}


public void registerContentObservers() {

    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));

}

 }
Run Code Online (Sandbox Code Playgroud)

viv*_*viv 19

只需看看你是否在清单文件中添加了此服务.......

谢谢.......


spe*_*ads 5

在清单中声明服务时,请尝试在清单中使用服务类的完整包位置.

例如. <service android:name="com.company.project.package.MyService">

我的服务没有启动,这对我有用.