Java Android清单中的提供者
我已经创建了一个SQLite位置内容提供程序。我已经把它放在我的舱单上了。唯一的问题是,在我打开应用程序中的某个活动之前,我不希望形成表。当前,LocationContentProvider在应用程序打开时自动生成表。我不希望发生这种情况,因为某些项目(如表名)直到我的应用程序中的某个活动才确定 为什么当我打开应用程序时,提供商会自动启动?我如何让它在某个特定活动中单击按钮后才启动 以下是我的位置ContentProvider:Java Android清单中的提供者,java,android,sqlite,android-contentprovider,Java,Android,Sqlite,Android Contentprovider,我已经创建了一个SQLite位置内容提供程序。我已经把它放在我的舱单上了。唯一的问题是,在我打开应用程序中的某个活动之前,我不希望形成表。当前,LocationContentProvider在应用程序打开时自动生成表。我不希望发生这种情况,因为某些项目(如表名)直到我的应用程序中的某个活动才确定 为什么当我打开应用程序时,提供商会自动启动?我如何让它在某个特定活动中单击按钮后才启动 以下是我的位置ContentProvider: public class LocationsContentProv
public class LocationsContentProvider extends ContentProvider{
public static final String PROVIDER_NAME = "com.blah.blah.blah";
/** A uri to do operations on locations table. A content provider is identified by its uri */
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );
/** Constant to identify the requested operation */
private static final int LOCATIONS = 1;
private static final UriMatcher uriMatcher ;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
}
/** This content provider does the database operations by this object */
LocationsDB mLocationsDB;
/** A callback method which is invoked when the content provider is starting up */
@Override
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;
}
/** A callback method which is invoked when insert operation is requested on this content provider */
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
if(rowID>0){
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
/** A callback method which is invoked when delete operation is requested on this content provider */
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 0;
cnt = mLocationsDB.del();
return cnt;
}
/** A callback method which is invoked by default content uri */
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(uriMatcher.match(uri)==LOCATIONS){
return mLocationsDB.getAllLocations();
}
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
}
这是我的数据库助手:
public class LocationsDB extends SQLiteOpenHelper{
/** Database name */
private static String DBNAME = "locationmarkersqlite";
/** Version number of the database */
private static int VERSION = 1;
/** Field 1 of the table locations, which is the primary key */
public static final String FIELD_ROW_ID = "_id";
/** Field 2 of the table locations, stores the latitude */
public static final String FIELD_LAT = "lat";
/** Field 3 of the table locations, stores the longitude*/
public static final String FIELD_LNG = "lng";
public static final String FIELD_BEAR = "bear";
public static final String FIELD_BOOMWIDTH = "boomwidth";
public static final String FIELD_COMMENTS = "comments";
/** Field 4 of the table locations, stores the zoom level of map*/
public static final String FIELD_ZOOM = "zom";
/** A constant, stores the the table name */
static String tablename = PolygonEngine.DATABASE_TABLE;
public static final String DATABASE_TABLE = "tablename";
/** An instance variable for SQLiteDatabase */
private SQLiteDatabase mDB;
/** Constructor */
public LocationsDB(Context context) {
super(context, DBNAME, null, VERSION);
this.mDB = getWritableDatabase();
}
/** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called
* provided the database does not exists
* */
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_BEAR + " double , " +
FIELD_BOOMWIDTH + " double , " +
FIELD_COMMENTS + " text , " +
FIELD_ZOOM + " text " +
" ) ";
db.execSQL(sql);
}
/** Inserts a new location to the table locations */
public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;
}
/** Deletes all locations from the table */
public int del(){
int cnt = mDB.delete(DATABASE_TABLE, null , null);
return cnt;
}
/** Returns all the locations from the table */
public Cursor getAllLocations(){
return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID, FIELD_LAT , FIELD_LNG, FIELD_BEAR, FIELD_BOOMWIDTH, FIELD_COMMENTS, FIELD_ZOOM } , null, null, null, null, null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
我的内容提供商自己启动时没有这个问题?但是,当应用程序向其请求某些内容时,它确实会启动。在我的启动活动中,我没有对提供程序的任何引用。并且您的代码中没有任何内容执行contentresolver()。查询?因为这是我的自定义提供者的第一个起点。