Android:将ContntProvider结果发送到Service并返回Activity

时间:2017-10-07 09:46:38

标签: android service cursor android-contentprovider

我有一些活动调用Service使用自定义ContentProvider来插入/更新/删除表中的条目。现在所有这些操作都有效,但活动没有收到任何结果。我现在想要的是也实现查询操作,但在这种情况下,我希望结果返回给调用者Activity。我该怎么做?

ContentProvider本身就像我同步使用它一样,因此它的代码无关紧要。 这是Service

public class TaskService extends IntentService {
    private static final String TAG = TaskService.class.getSimpleName();
    //Intent actions
    public static final String ACTION_INSERT = TAG + ".INSERT";
    public static final String ACTION_UPDATE = TAG + ".UPDATE";
    public static final String ACTION_DELETE = TAG + ".DELETE";
    public static final String ACTION_QUERY = TAG + ".QUERY";

    public static final String EXTRA_VALUES = TAG + ".ContentValues";

    public static void queryTasks(Context context, ContentValues values) {
        Intent intent = new Intent(context, TaskService.class);
        intent.setAction(ACTION_QUERY);
        intent.putExtra(EXTRA_VALUES, values);
        context.startService(intent);
    }

    public static void insertNewTask(Context context, ContentValues values) {
        Intent intent = new Intent(context, TaskService.class);
        intent.setAction(ACTION_INSERT);
        intent.putExtra(EXTRA_VALUES, values);
        context.startService(intent);
    }

    public static void updateTask(Context context, Uri uri, ContentValues values) {
        Intent intent = new Intent(context, TaskService.class);
        intent.setAction(ACTION_UPDATE);
        intent.setData(uri);
        intent.putExtra(EXTRA_VALUES, values);
        context.startService(intent);
    }

    public static void deleteTask(Context context, Uri uri) {
        Intent intent = new Intent(context, TaskService.class);
        intent.setAction(ACTION_DELETE);
        intent.setData(uri);
        context.startService(intent);
    }

    public TaskService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (ACTION_INSERT.equals(intent.getAction())) {
            ContentValues values = intent.getParcelableExtra(EXTRA_VALUES);
            performInsert(values);
        } else if (ACTION_UPDATE.equals(intent.getAction())) {
            ContentValues values = intent.getParcelableExtra(EXTRA_VALUES);
            performUpdate(intent.getData(), values);
        } else if (ACTION_DELETE.equals(intent.getAction())) {
            performDelete(intent.getData());
        } else if (ACTION_QUERY.equals(intent.getAction())) {
            performQuery(intent.getData());
        }
    }

    private void performInsert(ContentValues values) {
        if (getContentResolver().insert(DatabaseContract.CONTENT_URI, values) != null) {
            Log.d(TAG, "Inserted new task");
        } else {
            Log.w(TAG, "Error inserting new task");
        }
    }

    private void performQuery(Uri uri, ContentValues values) {
         //TODO
    }

    private void performUpdate(Uri uri, ContentValues values) {
        int count = getContentResolver().update(uri, values, null, null);
        Log.d(TAG, "Updated " + count + " task items");
    }

    private void performDelete(Uri uri) {
        int count = getContentResolver().delete(uri, null, null);

        Log.d(TAG, "Deleted "+count+" tasks");
    }

query()方法当然会返回Cursor。如何将此Cursor发送回Activity

0 个答案:

没有答案