android应用开发之清除通话记录

【一、粗略介绍】

通话记录的操作是通过ContactsProvider来玩的,具体的操作晚上大把的例子。摘录下主要部分:

数据库:/data/data/com.android.providers.contacts/databases/contacts2.db

表名:   calls

呼叫类型:

       来电:CallLog.Calls.INCOMING_TYPE (常量值:1)

       已拨:CallLog.Calls.OUTGOING_TYPE(常量值:2)

       未接:CallLog.Calls.MISSED_TYPE(常量值:3)

源代码中的provider的声明如下:

      packages\providers\ContactsProvider\AndroidManifest.xml

<provider android:name="CallLogProvider"
            android:authorities="call_log"
            android:syncable="false" android:multiprocess="false"
            android:exported="true"
            android:readPermission="android.permission.READ_CALL_LOG"
            android:writePermission="android.permission.WRITE_CALL_LOG">
        </provider>

勿忘声明权限袄

【二、实例】


ThreadCleanCallLogActivity

package cn.test.cleancalllog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.CallLog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class ThreadCleanCallLogActivity extends Activity implements
		OnClickListener {
	private boolean isExits = false;
	private boolean isGoOn = true;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			cleanCallLog();
		}

	};
	//其实就核心方法就这个方法中的几行代码。
	private void cleanCallLog() {
		ContentResolver resolver = getContentResolver();
		resolver.delete(CallLog.Calls.CONTENT_URI, null, null);
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("清除通话记录!");
		// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		init();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_about:
			Log.e("Other", "you click the about item!");
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_name)
					.setMessage("本程序由流浪天堂开发                               联系作者:[email protected]")
					.setCancelable(true)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
			break;

		case R.id.menu_exit:
			Log.e("Other", "you  click exit item!");
			finish();
			break;

		default:
			break;
		}
		return false;
	}

	private void init() {
		this.findViewById(R.id.btnThread).setOnClickListener(this);
	}

	public void onClick(View v) {
		if (v.getId() == R.id.btnThread) {
			if (!isExits) {
				isExits = true;
				new MyThread().start();
				Toast.makeText(getApplicationContext(), "删除完成!", 0).show();
			} else {
				Toast.makeText(getApplicationContext(), "删除中,请等待!", 0).show();
			}
		}

	}

	class MyThread extends Thread {

		public void run() {
			super.run();
			while (isGoOn) {
				try {
					sleep(30);
					Message msg = new Message();
					handler.sendMessage(msg);
					isGoOn = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/test">


    <Button
        android:id="@+id/btnThread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="threadhandler" 
        android:background="@drawable/buttonselect"
        android:gravity="center"
        />
</LinearLayout>


menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menu_about" android:title="关于" /> 
    <item android:id="@+id/menu_exit" android:title="退出" />
</menu>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.test.cleancalllog"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ThreadCleanCallLogActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>



你可能感兴趣的:(android应用开发之清除通话记录)