Java tutorial
/* 50AH-code ========= 50 Android Hacks (http://manning.com/sessa/) book source code Copyright (c) 2012 Manning Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /******************************************************************************* * Copyright (c) 2012 Manning * See the file license.txt for copying permission. ******************************************************************************/ package com.manning.androidhacks.hack043.service; import java.util.ArrayList; import android.app.IntentService; import android.content.ContentProviderOperation; import android.content.ContentProviderOperation.Builder; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.content.OperationApplicationException; import android.os.RemoteException; import android.util.Log; import com.manning.androidhacks.hack043.provider.MySQLContentProvider; public class SQLContentProviderService extends IntentService { private static final String TAG = SQLContentProviderService.class.getCanonicalName(); public SQLContentProviderService() { super(SQLContentProviderService.class.getSimpleName()); } public SQLContentProviderService(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { Builder builder = null; ContentResolver contentResolver = getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); builder = ContentProviderOperation.newDelete(MySQLContentProvider.CONTENT_URI); operations.add(builder.build()); for (int i = 1; i <= 100; i++) { ContentValues cv = new ContentValues(); cv.put(MySQLContentProvider.COLUMN_TEXT, "" + i); builder = ContentProviderOperation.newInsert(MySQLContentProvider.CONTENT_URI); builder.withValues(cv); operations.add(builder.build()); } try { contentResolver.applyBatch(MySQLContentProvider.AUTHORITY, operations); } catch (RemoteException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } catch (OperationApplicationException e) { Log.e(TAG, "Couldn't apply batch: " + e.getMessage()); } } }