標籤:遮擋號碼 刪除記錄
先說下思路 首先點擊撥號 進入一個是否撥通介面,選擇確認後,調用系統直接撥打到電話,然後判斷撥打到電話的狀態,如果是撥通的狀態下 用視窗管理器寫一個遮擋物 遮擋住 如果不是撥通狀態將遮擋物去掉,去掉完成當前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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1=(Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this,Main2.class);startActivityForResult(intent, 0); }});}@Overridepublic 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;}@Overridepublic 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() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container, false);return rootView;}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stub//manager.removeView(view);super.onDestroy();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.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;@Overrideprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfinish();}});}private void listionpho() {// TODO Auto-generated method stubTelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); PhoneStateListener listener=new PhoneStateListener(){@Overridepublic void onCallStateChanged(int state, String incomingNumber) {// TODO Auto-generated method stubswitch (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);}@Overridepublic 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;}@Overridepublic 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() {}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_main, container, false);return rootView;}}@Overrideprotected 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
仿360android打電話遮擋號碼與通話後記錄刪除