通話記錄的操作是通過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
[plain]
<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
[java]
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();
}
};
<span style="white-space:pre"> </span>//其實就核心方法就這個方法中的幾行代碼。
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
[plain]
<?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
[plain]
<?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
[plain]
<?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>