0

通話が行われるか、SMS が送信されるたびにデータベースに詳細を挿入するプログラムを実装したいと考えています。ブロードキャスト レシーバー、コンテンツ オブザーバー、または Service を使用する必要がありますか? 何が適切でしょうか?私はアンドロイドが初めてで、これについて緊急に助けが必要です。

私は今まで次のコードを実行しました。これに関する問題は、コードが初めて実行されたときに、たとえば通話ログに 8 つのレコードがあるため、これらの 8 つのレコードがデータベースに挿入されることです。次に、通話ログに変更が加えられた場合、たとえば別の電話をかけると、9 ではなく 17 のレコードがデータベースに表示されます。どこが間違っているのか教えてください。

    package com.calllogdb;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;




import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

//import static android.provider.BaseColumns._ID;
import static com.calllogdb.Constants.KEY_ContactNum ;
import static com.calllogdb.Constants.KEY_ContactName;
import static com.calllogdb.Constants.KEY_Duration;
import static com.calllogdb.Constants.KEY_Date ;
import static com.calllogdb.Constants.KEY_NumType ;
import static com.calllogdb.Constants.KEY_CurrTime ;
import static com.calllogdb.Constants.TABLE_NAME;

public class MainActivity extends Activity {

    private Helper helper = new Helper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new CallLogObserver(new Handler())); 



        try {

            addLog();
        }
    finally {

        helper.close();
    } 
    }

    private void addLog() {


        // TODO Auto-generated method stub
        SQLiteDatabase db;

         Cursor cursor = getContentResolver().query(
                    android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
                    android.provider.CallLog.Calls.DATE + " DESC ");

    db = helper.getWritableDatabase();


    startManagingCursor(cursor);
    int numberColumnId = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
   int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;



    ArrayList<String> callList = new ArrayList<String>();
    if (cursor.moveToFirst()) {
        do {
            String contactNumber = cursor.getString(numberColumnId);
            String contactName = cursor.getString(contactNameId);
            String duration = cursor.getString(durationId);
            String callDate = DateFormat.getDateInstance().format(dateId);
           String numType = cursor.getString(numTypeId);

            ContentValues values = new ContentValues();
            values.put(KEY_ContactName, contactName);
       values.put(KEY_NumType, numType);
            values.put(KEY_ContactNum, contactNumber);
            values.put(KEY_Duration, duration);
            values.put(KEY_Date, callDate);
            values.put(KEY_CurrTime, currTime);

            db.insert(TABLE_NAME, null, values);


            callList.add("Contact Number: " + contactNumber
                    + "\nContact Name: " + contactName + "\nDuration: "
                    + duration + "\nDate: " + callDate);


        } while (cursor.moveToNext());
}
    Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG).show();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }




    public class CallLogObserver extends ContentObserver
    {



        public CallLogObserver(Handler handler) {
            super(handler);
            // TODO Auto-generated constructor stub
        }


          @Override
            public boolean deliverSelfNotifications() {
                return true;
            }

            @Override
            public void onChange(boolean selfChange) {
                Log.d("LOG_TAG", "MyContentObserver.onChange( " + selfChange
                        + ")");
                super.onChange(selfChange);
                addLog();

            }





    }
}

電話をかけるたびに、通知を受け取り、最新のレコードだけでなく、レコードのセット全体を挿入します.これを防ぐにはどうすればよいですか? ありがとう

4

1 に答える 1