ContentProvider端代码:
package com.example.sqlitetest;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlitelayout);
final EditText id=(EditText) findViewById(R.id.EditId);
final EditText name=(EditText) findViewById(R.id.EditName);
final EditText score=(EditText) findViewById(R.id.EditScore);
final Button insert=(Button) findViewById(R.id.insert);
final Button read = (Button) findViewById(R.id.read);
final Button close=(Button) findViewById(R.id.close);
final EditText list=(EditText) findViewById(R.id.resultlist);
final SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/stu.db3", null);
db.execSQL("drop table if exists student");
db.execSQL("create table student(id integer,name varchar(50),score integer)");
insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String Id=id.getText().toString();
String Name=name.getText().toString();
String Score=score.getText().toString();
db.execSQL("insert into student values(?,?,?)",new String[]{Id,Name,Score} );
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
});
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list.setText("");
Cursor cursor=db.rawQuery("select * from student", null);
while (cursor.moveToNext()==true) {
int Id = cursor.getInt(0);
String Name=cursor.getString(1);
int Score=cursor.getInt(2);
list.append(Id+" "+Name+" "+Score+"\n");
}
}
});
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.close();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sqlitetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sqlitetest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name=".StuProvider"
android:authorities="com.example.sqlitetest.StuProvider"/>
</application>
</manifest>
ContentResolver端代码:
package com.example.contentresolver;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
Button button;
EditText readText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resolverlayout);
button=(Button) findViewById(R.id.getData);
readText=(EditText) findViewById(R.id.readData);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readText.setText("");
ContentResolver contentResolver=getContentResolver();
Uri uri =Uri.parse("content://com.example.sqlitetest.StuProvider");
System.out.println("1");
Cursor cursor=contentResolver.query(uri, null, null, null, null);
System.out.println("4");
while (cursor.moveToNext()==true) {
int Id = cursor.getInt(0);
String Name=cursor.getString(1);
int Score=cursor.getInt(2);
readText.append(Id+" "+Name+" "+Score+"\n");
}
}
});
}
}
点击按钮就报错,报安全异常:
------解决思路----------------------
在AndroidManifest.xml里面的provider中添加exported="true",即:
<provider android:name=".StuProvider"
android:authorities="com.example.sqlitetest.StuProvider"
android:exported="true"/>