先说下思路 首先点击拨号 进入一个是否拨通界面,选择确认后,调用系统直接拨打电话,然后判断拨打电话的状态,如果是拨通的状态下 用窗口管理器写一个遮挡物 遮挡住 如果不是拨通状态将遮挡物去掉,去掉完成当前activity,在onactivityforresult中执行查找通话记录进行删除。好了 开始上代码。
主界面mainactivity.class
拨号界面 mian2.class不要在意名字 随意写的
package com.example.callphone;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.os.Build;public class MainActivity extends ActionBarActivity { private Button button1; //private WindowManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,Main2.class); startActivityForResult(intent, 0); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } @Override protected void onDestroy() { // TODO Auto-generated method stub // manager.removeView(view); super.onDestroy(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(resultCode==1){ String num=data.getStringExtra("num"); deleteFromCallLog(num); } } public void deleteFromCallLog(String num) { Log.i("values", "删除呼叫记录:" + num); Uri uri = Uri.parse("content://call_log/calls");// 得到呼叫记录内容提供者的路径 Cursor cursor = getContentResolver().query(uri, new String[] { "_id" }, "number=?", new String[] { num }, null); while (cursor.moveToNext()) { String id = cursor.getString(0); getContentResolver().delete(uri, "_id=?", new String[] { id }); } cursor.close();}}
package com.example.callphone;import android.app.Activity;import android.support.v4.app.Fragment;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Gravity;import android.view.KeyEvent;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.view.WindowManager;import android.widget.Button;import android.widget.Toast;import android.os.Build;public class Main2 extends Activity { private Button btn_qs,btn_qr; //private WindowManager manager; private View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cellphone); btn_qr=(Button) findViewById(R.id.btn_qr); btn_qs=(Button) findViewById(R.id.btn_qs); view=View.inflate(this, R.layout.window, null); btn_qr.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"10086")); intent.putExtra("num", "10086"); setResult(1, intent); startActivity(intent); listionpho(); } }); btn_qs.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); } private void listionpho() { // TODO Auto-generated method stub TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); PhoneStateListener listener=new PhoneStateListener(){ @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub switch (state) { case TelephonyManager.CALL_STATE_IDLE: if (view != null && view.isShown()) { WindowManager windowManager = (WindowManager) getApplicationContext() .getSystemService(WINDOW_SERVICE); windowManager.removeView(view); finish(); } // manager=null; break; case TelephonyManager.CALL_STATE_OFFHOOK: //manager.removeView(view); showView(view); break; default: break; } } }; telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } protected void showView(View view2) { // TODO Auto-generated method stub //窗口管理器 WindowManager manager=(WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; // 设置行为选项 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.gravity=Gravity.TOP; params.width = WindowManager.LayoutParams.FILL_PARENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置显示初始位置 屏幕左上角为原点 // topWindow显示到最顶部 manager.addView(view2, params); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } @Override protected void onDestroy() { // TODO Auto-generated method stub // manager.removeView(view); super.onDestroy(); }}清单文件 注意权限 和mian2的主题风格
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.callphone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.READ_CALL_LOG"/> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.WRITE_CALL_LOG"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.callphone.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> <activity android:name="com.example.callphone.Main2" android:theme="@style/Transparent"></activity> </application></manifest>
colors.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="TRANS">#b0000000</color> <color name="white">#ffffffff</color></resources>
styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <style name="Transparent"> <item name="android:windowBackground">@color/TRANS</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item> </style></resources>
下载地址http://download.csdn.net/detail/u012303938/8539783