android135 360 来电去电归属地显示,自定义toast,
点击会开启服务。
- sivAddress.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (sivAddress.isChecked()) {
- sivAddress.setChecked(false);
- stopService(new Intent(SettingActivity.this,//只写this则表示OnClickListener,所以要写SettingActivity.this,
- AddressService.class));// 停止归属地服务
- } else {
- sivAddress.setChecked(true);
- startService(new Intent(SettingActivity.this,
- AddressService.class));// 开启归属地服务,就会调用AddressService的onCreate方法监听电话的状态。
- }
- }
- });
检测服务是否正在运行:
- package com.itheima52.mobilesafe.utils;
- import java.util.List;
- import android.app.ActivityManager;
- import android.app.ActivityManager.RunningServiceInfo;
- import android.app.ActivityManager.RunningTaskInfo;
- import android.content.Context;
- /**
- * 服务状态工具类
- *
- * // 根据归属地服务是否运行来更新checkbox
- boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this,
- "com.itheima52.mobilesafe.service.AddressService");//Service的包名类名
- */
- public class ServiceStatusUtils {
- /**
- * 检测服务是否正在运行
- */
- public static boolean isServiceRunning(Context ctx, String serviceName) {
- ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningServiceInfo> runningServices = am.getRunningServices(100);// 获取系统所有正在运行的服务,最多返回100个
- for (RunningServiceInfo runningServiceInfo : runningServices) {
- String className = runningServiceInfo.service.getClassName();// 获取服务的名称
- // System.out.println(className);
- if (className.equals(serviceName)) {// 服务存在
- return true;
- }
- }
- return false;
- }
- }
自定义组合控件:
- package com.itheima52.mobilesafe.view;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.itheima52.mobilesafe.R;
- /**
- * 设置中心的自定义组合控件
- *
- * <com.itheima52.mobilesafe.view.SettingClickView
- android:id="@+id/scv_address_location"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- private void initAddressStyle() {
- scvAddressStyle = (SettingClickView) findViewById(R.id.scv_address_style);
- scvAddressStyle.setTitle("归属地提示框风格");
- int style = mPref.getInt("address_style", 0);// 读取保存的style
- scvAddressStyle.setDesc(items[style]);
- scvAddressStyle.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showSingleChooseDailog();
- }
- });
- }
- */
- public class SettingClickView extends RelativeLayout {
- private TextView tvTitle;
- private TextView tvDesc;
- public SettingClickView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- initView();
- }
- public SettingClickView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initView();
- }
- public SettingClickView(Context context) {
- super(context);
- initView();
- }
- /**
- * 初始化布局
- */
- private void initView() {
- // 将自定义好的布局文件设置给当前的SettingClickView
- View.inflate(getContext(), R.layout.view_setting_click, this);
- tvTitle = (TextView) findViewById(R.id.tv_title);
- tvDesc = (TextView) findViewById(R.id.tv_desc);
- }
- public void setTitle(String title) {
- tvTitle.setText(title);
- }
- public void setDesc(String desc) {
- tvDesc.setText(desc);
- }
- }
来去电归属地显示,自定义弹框:
- package com.itheima52.mobilesafe.service;
- import com.itheima52.mobilesafe.R;
- import com.itheima52.mobilesafe.db.dao.AddressDao;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.graphics.PixelFormat;
- import android.os.IBinder;
- import android.telephony.PhoneStateListener;
- import android.telephony.TelephonyManager;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * 来电提醒的服务
- * 清单文件注册:<service android:name=".service.AddressService" >
- *
- */
- public class AddressService extends Service {
- private TelephonyManager tm;
- private MyListener listener;
- private OutCallReceiver receiver;
- private WindowManager mWM;
- private View view;
- private SharedPreferences mPref;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- mPref = getSharedPreferences("config", MODE_PRIVATE);
- //来电是通过TelephonyManager服务来监听,去电是通过广播接受者来接收,这是安卓的api决定的。
- tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
- listener = new MyListener();
- tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);// 监听来电的状态,
- receiver = new OutCallReceiver();
- IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
- registerReceiver(receiver, filter);// 动态注册广播
- }
- class MyListener extends PhoneStateListener {
- @Override
- public void onCallStateChanged(int state, String incomingNumber) {
- switch (state) {
- case TelephonyManager.CALL_STATE_RINGING:// 电话铃声响了
- System.out.println("电话铃响...");
- String address = AddressDao.getAddress(incomingNumber);// 根据来电号码查询归属地
- // Toast.makeText(AddressService.this, address,
- // Toast.LENGTH_LONG)
- // .show();
- showToast(address);
- break;
- case TelephonyManager.CALL_STATE_IDLE:// 电话闲置状态,挂断电话的时候消除showToast()方法产生的WindowManager窗口。
- if (mWM != null && view != null) {
- mWM.removeView(view);// 从window中移除view,否则这个窗口会一直显示在手机屏幕。
- view = null;
- }
- break;
- default:
- break;
- }
- super.onCallStateChanged(state, incomingNumber);
- }
- }
- /**
- * 监听去电的广播接受者 需要权限: android.permission.PROCESS_OUTGOING_CALLS
- */
- class OutCallReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String number = getResultData();// 获取去电电话号码
- String address = AddressDao.getAddress(number);
- Toast.makeText(context, address, Toast.LENGTH_LONG).show();
- showToast(address);
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();//停掉本AddressService服务,但是他里面的tm服务监听没有停止,
- tm.listen(listener, PhoneStateListener.LISTEN_NONE);// 停止来电监听
- unregisterReceiver(receiver);// 注销去电广播
- }
- /**
- * 自定义归属地浮窗
- */
- private void showToast(String text) {
- mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);//系统服务,WindowManager可以一直显示在手机屏幕上。
- //模仿的Toast源码
- WindowManager.LayoutParams params = new WindowManager.LayoutParams();
- params.height = WindowManager.LayoutParams.WRAP_CONTENT;
- params.width = WindowManager.LayoutParams.WRAP_CONTENT;
- params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
- | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
- params.format = PixelFormat.TRANSLUCENT;
- params.type = WindowManager.LayoutParams.TYPE_TOAST;
- params.setTitle("Toast");
- // view = new TextView(this);
- view = View.inflate(this, R.layout.toast_address, null);
- /*<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/call_locate_blue"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@android:drawable/ic_menu_call" /> 安卓的图片
- <TextView
- android:id="@+id/tv_number"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="未知号码"
- android:textSize="18sp"
- android:textColor="#fff" />
- </LinearLayout>*/
- int[] bgs = new int[] { R.drawable.call_locate_white,
- R.drawable.call_locate_orange, R.drawable.call_locate_blue,
- R.drawable.call_locate_gray, R.drawable.call_locate_green };
- int style = mPref.getInt("address_style", 0);
- view.setBackgroundResource(bgs[style]);// 根据存储的样式更新背景
- TextView tvText = (TextView) view.findViewById(R.id.tv_number);
- tvText.setText(text);
- mWM.addView(view, params);// 将view添加在屏幕上(Window,所有的控件都显示在window上,在windoew上面可以显示Activity和fragment,)
- }
- }
android135 360 来电去电归属地显示,自定义toast,的更多相关文章
- 自定义Toast和RatingBar的简单用例
Toast是一个包含用户点击消息.Toast类会帮助你创建和显示这些.Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动 向右划动五角星增加 单击按钮显示自 ...
- Android MTK平台 客制化系统来电界面(屏蔽 InCallUI 提供接口给客户自行展示来电去电页面)
OS: Android 8.1 需求分析 1.禁止系统来电铃声,提供接口给客户自己播放铃声 2.禁止系统拉起来去电页面(InCallActivity),消息通知客户拉起自己的来去电页面 3.禁止来电消 ...
- ASP.NET Core中显示自定义错误页面-增强版
之前的博文 ASP.NET Core中显示自定义错误页面 中的方法是在项目中硬编码实现的,当有多个项目时,就会造成不同项目之间的重复代码,不可取. 在这篇博文中改用middleware实现,并且放在独 ...
- ASP.NET Core中显示自定义错误页面
在 ASP.NET Core 中,默认情况下当发生500或404错误时,只返回http状态码,不返回任何内容,页面一片空白. 如果在 Startup.cs 的 Configure() 中加上 app. ...
- 判断数据库内容,在页面显示自定义数据case when
判断数据库内容,在页面显示自定义数据 case when...then ...else...end 比如:数据库内容是这样: 通过sql语句判断,数据库的name字段,内容是月桂的,显示嫦娥,其他的显 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- DedeCMS中实现在顶层banner中显示自定义登录信息
一.需求描述 dedeCMS自带的模板中有互动中心模块,如下图所示: 由于会员登陆对我来说不是网站的重要模块且默认DedeCMS的会员中心模块的初始化很慢,常会显示“正在载入中,请稍候...”, 所以 ...
- 双击CAD对象(具有扩展数据),显示自定义对话框实现方法
转自:Cad人生 链接:http://www.cnblogs.com/cadlife/p/3463337.html 题目:双击CAD对象,显示自定义对话框实现方法 内容粘贴如下: 主要是绑定两个事件: ...
- ToastUtil【简单的Toast封装类】【未自定义Toast的显示风格】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 一个简单的Toast封装类. 效果图 API = 6.0 API = 4.4.2 代码分析 实现了不管我们触发多少次Toast调用, ...
随机推荐
- 漫谈MySql中的事务
最近一直在做订单类的项目,使用了事务.我们的数据库选用的是MySql,存储引擎选用innoDB,innoDB对事务有着良好的支持.这篇文章我们一起来扒一扒事务相关的知识. 为什么要有事务? 事务广泛的 ...
- (转)MySQL数据库引擎ISAM MyISAM HEAP InnoDB的区别
转自:http://blog.csdn.net/nightelve/article/details/16895917 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.4
(1). There is a natural isomorphism between the spaces $\scrH\otimes \scrH^*$ and $\scrL(\scrH,\scrK ...
- create a C# context menu from code
I try the one of your approach, it works well in my computer. Below is my code: public void AddConte ...
- Codeforces 629C Famil Door and Brackets DP
题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...
- PICT实现组合测试用例(二)
上次简单总结了PICT命令的一些用法,这次重新把<软件测试实战>里面有关这一章的内容再总结一次,以巩固理解. 组合测试的概念 组合测试(combinatorial testing)是一种测 ...
- js实现收缩菜单效果
废话不多说,直接上代码: 有注释 <head> <title></title> <style type="text/css"> di ...
- C++设计模式——策略模式
策略模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化 ...
- sqlite 修改表名,合并数据库(文件)
修改表名:ALTER TABLE orig_table_name RENAME TO tmp_table_name; 将某个数据库的一个表的数据插入到另一个数据库的某个表里:1.先连接数据库A2.再a ...
- Java网络编程(UDP协议:发送端)
package WebProgramingDemo; import java.io.IOException; import java.net.DatagramPacket; import java.n ...