我尝试将所有通话记录复制到创建的数据库并且代码运行良好,但以下包中没有.db
个文件,
data/data/your.application.package/databases/
班级代码:
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Date;
import java.text.DateFormat;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.provider.CallLog;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class logs extends BroadcastReceiver
{
SQLiteDatabase calllog;
String get_number, get_type, get_duration, Current_time, get_date,logTypecode;
Handler hand = new Handler();
Cursor cursor;
LocalDb dbex;
ContentValues cm;
int logtype;
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(intent.getAction().equals("android.intent.action.PHONE_STATE"))
{
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
hand.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
String number=android.provider.CallLog.Calls.NUMBER;
String date=android.provider.CallLog.Calls.DATE;
String type=android.provider.CallLog.Calls.TYPE;
String duration=android.provider.CallLog.Calls.DURATION;
Log.i("ddd", ""+duration);
Cursor cursor=context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if(cursor.moveToNext()){
cursor.moveToPrevious();
while(cursor.moveToNext()){
get_number=cursor.getString(cursor.getColumnIndex(number));
get_date=cursor.getString(cursor.getColumnIndex(date));
get_type=cursor.getString(cursor.getColumnIndex(type));
get_duration=cursor.getString(cursor.getColumnIndex(duration));
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-dd-mm HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
logtype=Integer.parseInt(get_type);
Current_time=DateFormat.getDateTimeInstance().format(new Date());
Log.i("ddd", ""+get_duration);
try
{
switch (logtype) {
case CallLog.Calls.OUTGOING_TYPE:
logTypecode="O";
dbex.open();
Log.i("ddd", ""+get_duration);
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
case CallLog.Calls.INCOMING_TYPE:
logTypecode="I";
dbex.open();
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
case CallLog.Calls.MISSED_TYPE:
logTypecode="M";
dbex.open();
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
}
}
catch(Exception e)
{
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
cursor.close();
}
},2000);
}
}
}
}
logs.java的DBAdapter Helper类
public class LocalDb {
private static final int DATABASE_VERSION=3;
private DatabaseHelper dbhelp;
public SQLiteDatabase mdb;
static final String DATABASE_NAME ="DBImage";
static final String Logs =" Logs";
Context context;
private static final String Table1 = "create table if not exists " + Logs + "( id integer primary key autoincrement ," + "number text,type text,time text,duration text,date text )";
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(Table1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+Table1);
}
}
public LocalDb(Context ctx){
this.context=ctx;
}
public LocalDb open() throws SQLException{
dbhelp=new DatabaseHelper(context);
mdb=dbhelp.getWritableDatabase();
return this;
}
public void close(){
dbhelp.close();
}
public long insert_values(String Table,ContentValues initialvalues){
return mdb.insert(Table, null, initialvalues);
}
public Cursor fetchMainLists(){
String sql = "SELECT id,number,type,time,duration,date FROM Logs ";
Cursor cur = mdb.rawQuery(sql, null);
return cur ;
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ekp.call_log_db"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_INPUT_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="logs" >
<intent-filter android:priority="100" >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver><activity android:name=".MainActivity" android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
12-22 15:18:29.965: E/Database(1676): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
12-22 15:18:29.965: E/Database(1676): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
12-22 15:18:29.965: E/Database(1676): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
12-22 15:18:29.965: E/Database(1676): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
12-22 15:18:29.965: E/Database(1676): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
12-22 15:18:29.965: E/Database(1676): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
12-22 15:18:29.965: E/Database(1676): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-22 15:18:29.965: E/Database(1676): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-22 15:18:29.965: E/Database(1676): at com.ekp.call_log.logs.onReceive(logs.java:35)
12-22 15:18:29.965: E/Database(1676): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
12-22 15:18:29.965: E/Database(1676): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
12-22 15:18:29.965: E/Database(1676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
12-22 15:18:29.965: E/Database(1676): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 15:18:29.965: E/Database(1676): at android.os.Looper.loop(Looper.java:123)
12-22 15:18:29.965: E/Database(1676): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 15:18:29.965: E/Database(1676): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 15:18:29.965: E/Database(1676): at java.lang.reflect.Method.invoke(Method.java:507)
12-22 15:18:29.965: E/Database(1676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 15:18:29.965: E/Database(1676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 15:18:29.965: E/Database(1676): at dalvik.system.NativeStart.main(Native Method)
数据库 - 不将值插入数据库表
12-22 16:53:55.145: E/Database(30202): close() was never explicitly called on database '/data/data/com.ekp.call_log_db/databases/DBImage'
12-22 16:53:55.145: E/Database(30202): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
12-22 16:53:55.145: E/Database(30202): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
12-22 16:53:55.145: E/Database(30202): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
12-22 16:53:55.145: E/Database(30202): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
12-22 16:53:55.145: E/Database(30202): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
12-22 16:53:55.145: E/Database(30202): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
12-22 16:53:55.145: E/Database(30202): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-22 16:53:55.145: E/Database(30202): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-22 16:53:55.145: E/Database(30202): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
12-22 16:53:55.145: E/Database(30202): at com.ekp.call_log_db.LocalDb.open(LocalDb.java:56)
12-22 16:53:55.145: E/Database(30202): at com.ekp.call_log_db.logs$1.run(logs.java:46)
12-22 16:53:55.145: E/Database(30202): at android.os.Handler.handleCallback(Handler.java:587)
12-22 16:53:55.145: E/Database(30202): at android.os.Handler.dispatchMessage(Handler.java:92)
12-22 16:53:55.145: E/Database(30202): at android.os.Looper.loop(Looper.java:123)
12-22 16:53:55.145: E/Database(30202): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 16:53:55.145: E/Database(30202): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 16:53:55.145: E/Database(30202): at java.lang.reflect.Method.invoke(Method.java:507)
12-22 16:53:55.145: E/Database(30202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 16:53:55.145: E/Database(30202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 16:53:55.145: E/Database(30202): at dalvik.system.NativeStart.main(Native Method)
由于
答案 0 :(得分:0)
写下接收器代码而不是广播接收器代码,它将解决您的问题。
public class logs extends BroadcastReceiver {
SQLiteDatabase calllog;
String get_number, get_type, get_duration, Current_time, get_date,logTypecode;
Handler hand = new Handler();
Cursor cursor;
LocalDb dbex;
ContentValues cm;
int logtype;
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
dbex = new LocalDb(context);
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
hand.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
String number=android.provider.CallLog.Calls.NUMBER;
String date=android.provider.CallLog.Calls.DATE;
String type=android.provider.CallLog.Calls.TYPE;
String duration=android.provider.CallLog.Calls.DURATION;
Log.i("ddd", ""+duration);
dbex.open();
Cursor cursor=context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if(cursor.moveToNext()) {
cursor.moveToPrevious();
while(cursor.moveToNext()) {
get_number=cursor.getString(cursor.getColumnIndex(number));
get_date=cursor.getString(cursor.getColumnIndex(date));
get_type=cursor.getString(cursor.getColumnIndex(type));
get_duration=cursor.getString(cursor.getColumnIndex(duration));
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-dd-mm HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
logtype=Integer.parseInt(get_type);
Current_time=DateFormat.getDateTimeInstance().format(new Date());
Log.i("ddd", ""+get_duration);
try {
switch (logtype) {
case CallLog.Calls.OUTGOING_TYPE:
logTypecode="O";
dbex.open();
Log.i("ddd", ""+get_duration);
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
case CallLog.Calls.INCOMING_TYPE:
logTypecode="I";
dbex.open();
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
case CallLog.Calls.MISSED_TYPE:
logTypecode="M";
dbex.open();
cm.put("number",get_number);
cm.put("type",get_type);
cm.put("time",Current_time);
cm.put("duration",get_duration);
cm.put("date",get_date);
dbex.insert_values("Logs",cm);
dbex.close();
break;
}
} catch(Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
cursor.close();
dbex.close();
}
},2000);
}
}
}
}