本文主要是介绍来去电归属地入门学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<pre name="code" class="java"><pre code_snippet_id="424023" snippet_file_name="blog_20140710_1_8523739" name="code" class="java"><pre name="code" class="java">思路:>1.进入系统界面时是否开启归属地显示服务
>在MainActivity中:
/*** 来电 归属地显示服务*/SharedPreferences sp = getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);boolean callflag = sp.getBoolean(Constant.CALL_ADDRESS, true);if(callflag){// 归属地显示服务Intent showAddressService = new Intent();showAddressService.setAction("li.intent.category.ShowAddressService");// Context.startService(Intent service)startService(showAddressService);}>2,开启服务 a1.注册广播 [拦截一个外拨的广播 ],b.监听通话状态[TelephonyManager]
>3监听处理得到号码查询数据库取得归属地<span style="white-space:pre"> </span>显示WindowManager<span style="white-space:pre"> </span>//可以布局参数 // 加入布局 windowmg.addView(view, params)
>4记得要 onDestroy时 3.回调:条件 stopService 仅有一次// 移除注册unregisterReceiver(outCallReceiver); <// 移除话状态监听
public class ShowAddressService extends Service {中://来电归属地显示背景int[] styleitems = { R.drawable.call_locate_white,//R.drawable.call_locate_orange, R.drawable.call_locate_blue, R.drawable.call_locate_green, R.drawable.call_locate_gray };//归属地显示风格int[] styleViews = { R.layout.toast_show_address,//R.layout.toast_show_address1 };<pre name="code" class="java">// 1.回调:条件 startService 仅有一次@Overridepublic void onCreate() {super.onCreate();System.out.println("--归属地服务----onCreate---创建");windowmg = (WindowManager) getSystemService(Context.WINDOW_SERVICE);// 拦截// 1.外拨 [拦截一个外拨的广播 ]// #########广播的注册方式有两种1.1xml注册 全局 系统先解析后初始 <receiver1.2代码注册 局部 手动初始化// 创建过滤器IntentFilter filter = new IntentFilter();// 广播类型filter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);filter.setPriority(1000);// 注册广播registerReceiver(outCallReceiver, filter);// 2.来电// ##############TelephonyManager 通讯相关的服务 sim/通话状态 监听器拦截的manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// manager.setON...Listener();// 监听通话状态manager.listen(mPhoneStateListener, // 通话状态监听器PhoneStateListener.LISTEN_CALL_STATE// 设置监听类型);练习代码:
<pre name="code" class="java">// 拦截外拨的接收者private BroadcastReceiver outCallReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent) {String number = super.getResultData();dealNumber(number);}};// 通话状态监听器 private PhoneStateListener mPhoneStateListener = new PhoneStateListener(){public void onCallStateChanged(int state,String incomingNumber) //来电号码{switch(state){case TelephonyManager.CALL_STATE_IDLE: // 没有电话dismissDialog();break;case TelephonyManager.CALL_STATE_OFFHOOK: // 接听break;case TelephonyManager.CALL_STATE_RINGING: // 响铃dealNumber(incomingNumber);break;}}};<pre name="code" class="java">/*** 处理号码归属地查询* @param incomingNumber*/private void dealNumber(String incomingNumber) {PhoneNumberDao dao = new PhoneNumberDao();String address = dao.getNumberAddress(incomingNumber);showDialog("归属地"+address);}/*** 来电归属地显示* @param address*/private int height = 0;private int width = 0;private void showDialog(String address) {SharedPreferences sp = getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);int styleView = sp.getInt(Constant.ADDRESS_VIEW_STYLE,0);view = View.inflate(getBaseContext(), styleViews[styleView], null);// ----------------使用风格// "半透明", "活力橙", "卫士蓝", "苹果绿", "金属灰"int bgstyle = sp.getInt(Constant.ADDRESS_STYLE, 0);view.setBackgroundResource((styleitems[bgstyle]));TextView gsd_edt = (TextView) view.findViewById(R.id.tv_toast_address);gsd_edt.setText(address);// windowr的布局参数params = new WindowManager.LayoutParams();// 对齐params.gravity = Gravity.LEFT | Gravity.TOP;params.height = WindowManager.LayoutParams.WRAP_CONTENT;params.width = WindowManager.LayoutParams.WRAP_CONTENT;// 类型值params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE// 不能获取焦点// | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE// 不能touch| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;// 屏幕高亮// 背景格式params.format = PixelFormat.TRANSLUCENT;// translucent透明// 视图类型 调节层次params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;// --------------------获取保存坐标int x = sp.getInt(Constant.RAW_X, 0);int y = sp.getInt(Constant.RAW_Y, 0);params.x = x-width/2;params.y = y-height/2;// 加入布局windowmg.addView(view, params);/*** */view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) { // 事件 动作 坐标switch(event.getAction()){case MotionEvent.ACTION_DOWN: height = view.getBottom() - view.getTop();width = view.getRight() - view.getLeft();break;case MotionEvent.ACTION_MOVE:params.x = (int) (event.getRawX() - width/2);params.y = (int) (event.getRawY() - height);windowmg.updateViewLayout(view, params);break;case MotionEvent.ACTION_UP:break;}return false;}});};/*** 隐藏对话框*/public void dismissDialog(){if(view != null){if(view.getParent()!=null){windowmg.removeView(view);view = null;}}}@Overridepublic void onDestroy() {// 移除注册unregisterReceiver(outCallReceiver);manager.listen(mPhoneStateListener, // 通话状态监听器PhoneStateListener.LISTEN_NONE);// 设置监听类型super.onDestroy();}
效果图:
<img src="https://img-blog.csdn.net/20140710175924204?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE5MTg2MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
这篇关于来去电归属地入门学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!