本文的目的是介绍在Toast中自定义Android对讲的详细情况,特别关注android自定义toast的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解在Toast中
本文的目的是介绍在Toast中自定义Android对讲的详细情况,特别关注android 自定义toast的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解在Toast中自定义Android对讲的机会,同时也不会遗漏关于023 Android 自定义Toast控件、Android Toast 自定义布局、android – 自定义Toast取消不起作用、Android 下自定义 Toast的知识。
本文目录一览:- 在Toast中自定义Android对讲(android 自定义toast)
- 023 Android 自定义Toast控件
- Android Toast 自定义布局
- android – 自定义Toast取消不起作用
- Android 下自定义 Toast
在Toast中自定义Android对讲(android 自定义toast)
我已经通过Android TalkBack检查了所有默认的Toast消息.默认的Android Talkback行为是读取Toast中的所有内容(不间断).有什么办法可以根据需要自定义它.例如 :
Toast.makeText(getApplicationContext(), "Hello World! Welcome to Android. This is Stackoverflow.", Toast.LENGTH_SHORT).show();
当Toast出现时,它会自动显示“ Hello world!欢迎使用Android.这是Stackoverflow.”但是我想控制它,就像它应该只读“ Hello world”或“ Welcome to Android”或“ This is Stackoverflow”等.
解决方法:
我想不出这样做的理由-通常,您希望在提供给用户的信息之间有某种平价,无论他们使用信息的方式如何.
如果您只是想通过TalkBack朗读文本,也许可以直接使用View.announceForAccessibility API.那个…
是的,语音反馈可访问性服务可能会大声朗读某些内容,而可视化呈现的内容可能有所不同.
Toast toast = Toast.makeText(this, "visible text", Toast.LENGTH_SHORT);
View.AccessibilityDelegate delegate = new View.AccessibilityDelegate() {
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
super.onPopulateAccessibilityEvent(host, event);
event.setContentDescription("audible text");
}
};
((ViewGroup) toast.getView()).getChildAt(0).setAccessibilityDelegate(delegate);
toast.show();
请注意获得引用TextView的超级技巧.这很危险,因为它可能会在Android的不同实现上中断.
您可以直接在Toast上设置自己的视图:
Toast toast = new Toast(this);
TextView messageView = new TextView(this);
messageView.setText("visible text");
View.AccessibilityDelegate delegate = new View.AccessibilityDelegate() {
@Override
public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
super.onPopulateAccessibilityEvent(host, event);
event.setContentDescription("audible text");
}
};
messageView.setAccessibilityDelegate(delegate);
toast.setView(messageView);
toast.show();
023 Android 自定义Toast控件
1.Toast自定义控件工具类
package com.example.administrator.test62360safeguard.Utils;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.administrator.test62360safeguard.R;
public class ToastUtil {
/**
* 显示吐司
* @param context 应用的上下文
* @param windowManager 窗口管理器
* @param textshow 需要在textview 中需要显示的文本内容
* @return 吐司toast控件
*/
public static View showToast(final Context context, final WindowManager windowManager, String textshow){
final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
final WindowManager.LayoutParams params = mParams;
//设置toast的高度、宽度
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; //默认可以被触摸
params.gravity=Gravity.LEFT+Gravity.TOP; //设置toast的位置()左上角
//toast显示效果,xml--->view ,将toast挂到windowManager窗体
final View myToastView = View.inflate(context, R.layout.toast_view, null);
//给自定义的toast控件添加拖拽事件
myToastView.setOnTouchListener(new View.OnTouchListener() {
int startX;
int startY;
int window_width;
int window_height;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX(); //获取点击位置(移动前)相对于整个屏幕原点(左上)的x距离
startY = (int) event.getRawY(); //获取点击位置(移动前)相对于整个屏幕原点(左上)的y距离
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX(); //获取点击位置(移动后)相对于整个屏幕原点(左上)的x距离
int moveY = (int) event.getRawY();//获取点击位置(移动后)相对于整个屏幕原点(左上)的y距离
int disX=moveX-startX; //x方向的移动距离
int disY=moveY-startY; //y方向的移动距离
params.x=params.x+disX; //设置toast控件的放置位置
params.y=params.y+disY;
//容错处理
//获取屏幕的宽和高
window_width = windowManager.getDefaultDisplay().getWidth();
window_height=windowManager.getDefaultDisplay().getHeight();
//以下4个if语句:判断控件是否在屏幕的有效区域内
if(params.x<0){
params.x=0;
}
if(params.y<0){
params.y=0;
}
if(params.x>window_width-myToastView.getWidth()){
params.x=window_width-myToastView.getWidth();
}
if(params.y>window_height-myToastView.getHeight()){
params.y=window_height-myToastView.getHeight();
}
//告知窗体,toast控件需要按照手势的移动,去做位置的更新
windowManager.updateViewLayout(myToastView,params);
startX = (int) event.getRawX(); //获取点击位置(移动后)相对于整个屏幕原点(左上)的x距离
startY = (int) event.getRawY(); //获取点击位置(移动后)相对于整个屏幕原点(左上)的y距离
break;
case MotionEvent.ACTION_UP:
SharePreferenceUtil.putInt(context, ConstantValue.LOCATION_X, params.x);
SharePreferenceUtil.putInt(context, ConstantValue.LOCATION_Y, params.y);
break;
}
//(1)若只需要响应拖拽事件,应该返回true
//(2)既要响应点击事件,又要响应拖拽过程,则此返回值结果需要修改为false
return true;
}
});
params.x=SharePreferenceUtil.getInt(context,ConstantValue.LOCATION_X,0);
params.y=SharePreferenceUtil.getInt(context,ConstantValue.LOCATION_Y,0);
int[] picArray=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 toast_style_index = SharePreferenceUtil.getInt(context, ConstantValue.TOAST_STYLE, 0);
//设置toast中显示的文字内容
TextView tv_toast = myToastView.findViewById(R.id.tv_toast);
tv_toast.setText(textshow);
tv_toast.setBackgroundResource(picArray[toast_style_index]);
//在窗体上挂载一个view(需要添加权限)
windowManager.addView(myToastView,mParams);
return myToastView;
}
/**
* 移除吐司
* @param windowManager 窗口管理器
* @param toastView 所要关闭的toast组件
*/
public static void closeToast( WindowManager windowManager, View toastView) {
windowManager.removeView(toastView);
}
}
2.调用类
(1)设置中心页面的后台处理类:SettingActivity.java
package com.example.administrator.test62360safeguard;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import com.example.administrator.test62360safeguard.Utils.ConstantValue;
import com.example.administrator.test62360safeguard.Utils.SetttingClickView;
import com.example.administrator.test62360safeguard.Utils.SetttingItemView;
import com.example.administrator.test62360safeguard.Utils.SharePreferenceUtil;
import com.example.administrator.test62360safeguard.Utils.ToastUtil;
public class SettingActivity extends AppCompatActivity {
View toastView=null;
private String[] toastStyleArray;
private int toast_style_value;
private SetttingClickView scv_toast_style;
private SetttingClickView scv_toast_location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
initUpdate();
initPhoneAddress();
initToastStyle();
initToastLocation();
}
/**
* 设置中心列表中第4个条目:设置归属地提示框位置
*/
private void initToastLocation() {
scv_toast_location = findViewById(R.id.scv_toast_location);
scv_toast_location.setTitle("归属地提示框位置");
scv_toast_location.setDes("设置归属地提示框位置");
scv_toast_location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),ToastLocationActivity.class);
startActivity(intent);
}
});
}
/**
* 设置中心列表中第3个条目:设置归属地显示风格
*/
private void initToastStyle() {
scv_toast_style = findViewById(R.id.scv_toast_style);
scv_toast_style.setTitle("设置归属地显示风格");
//1.创建描述文字所在的string类型的数组
toastStyleArray = new String[]{"透明","橙色","蓝色","灰色","绿色"};
//2.通过sp中存储的显示样式对应的索引值,用于获取描述文字
toast_style_value = SharePreferenceUtil.getInt(getApplicationContext(),ConstantValue.TOAST_STYLE,0);
//3.通过索引,获取字符串数组中的文字,让控件显示
scv_toast_style.setDes(toastStyleArray[toast_style_value]);
//4.给自定义控件绑定点击事件
scv_toast_style.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastStyleDialog();
}
});
}
/**
* 创建选择toast显示样式对话框
*/
private void showToastStyleDialog() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher); //设置对话框图标
builder.setTitle("请选择归属地样式"); //设置标题
/*
选择单个条目事件监听
参数1:string类型的数组描述颜色
参数2:弹出对话框选中条目的索引值
参数3:点击某一个条目后触发的点击事件(1.记录选择的索引值 2.关闭对话框 3.显示选中颜色的文字)
*/
builder.setSingleChoiceItems(toastStyleArray, toast_style_value, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//(1.记录选择的索引值 2.关闭对话框 3.显示选中颜色的文字)
SharePreferenceUtil.putInt(getApplicationContext(),ConstantValue.TOAST_STYLE,which);
dialog.dismiss(); //关闭对话框
scv_toast_style.setDes(toastStyleArray[which]);
}
});
//消极按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show(); //显示对话框
}
/**
* 设置中心列表中第2个条目:是否显示电话号码归属地的方法
*/
private void initPhoneAddress() {
final SetttingItemView siv_phone_address=findViewById(R.id.siv_phone_address);
//获取已有的开关状态,用来显示
boolean open_phone=SharePreferenceUtil.getBoolean(this,ConstantValue.OPEN_PHONE,false);
siv_phone_address.setChecked(open_phone);
//点击过程中,状态切换过程
siv_phone_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取之前的选中状态
boolean isCheck=siv_phone_address.isChecked();
//将原有状态取反,设置为当前状态
siv_phone_address.setChecked(!isCheck);
//将当前状态保存到SharePreference中(进行更新)
SharePreferenceUtil.putBoolean(getApplicationContext(),ConstantValue.OPEN_PHONE,!isCheck);
WindowManager windowManager= (WindowManager) getSystemService(WINDOW_SERVICE);
if(!isCheck){
//调用自定义toast
toastView=ToastUtil.showToast(getApplicationContext(),windowManager,"hello linda");
}else {
if(toastView!=null){
ToastUtil.closeToast(windowManager,toastView);
}
}
}
});
}
/**
* 设置中心列表中第1个条目:自动更新模块
*/
private void initUpdate() {
final SetttingItemView siv_update=findViewById(R.id.siv_update);
//获取已有的开关状态,用来显示
boolean open_update=SharePreferenceUtil.getBoolean(this,ConstantValue.OPEN_UPDATE,false);
siv_update.setChecked(open_update);
siv_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取之前的选中状态
boolean isCheck=siv_update.isChecked();
//将原有状态取反,设置为当前状态
siv_update.setChecked(!isCheck);
//将当前状态保存到SharePreference中(进行更新)
SharePreferenceUtil.putBoolean(getApplicationContext(),ConstantValue.OPEN_UPDATE,!isCheck);
}
});
}
}
(2)设置Toast控件显示位置的页面后台代码:ToastLocationActivity.java
package com.example.administrator.test62360safeguard;
import android.annotation.SuppressLint;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.example.administrator.test62360safeguard.Utils.ConstantValue;
import com.example.administrator.test62360safeguard.Utils.SharePreferenceUtil;
public class ToastLocationActivity extends AppCompatActivity {
ImageView iv_drag;
Button bt_drag_visible;
Button bt_drag_invisible;
private int startX;
private int startY;
private WindowManager windowManager;
private int window_width;
private int window_height;
private long[] mHits = new long[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_location);
initUI();
}
@SuppressLint("ClickableViewAccessibility")
private void initUI() {
iv_drag = findViewById(R.id.ivTL_drag_pic);
bt_drag_visible = findViewById(R.id.bt_drag_visible);
bt_drag_invisible = findViewById(R.id.bt_drag_invisible);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
window_width = windowManager.getDefaultDisplay().getWidth();
window_height=windowManager.getDefaultDisplay().getHeight();
int location_x=SharePreferenceUtil.getInt(getApplicationContext(),ConstantValue.LOCATION_X,0);
int location_y=SharePreferenceUtil.getInt(getApplicationContext(),ConstantValue.LOCATION_Y,0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin=location_x;
layoutParams.topMargin=location_y;
iv_drag.setLayoutParams(layoutParams);
//给控件绑定双击事件
iv_drag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
mHits[mHits.length-1] = SystemClock.uptimeMillis();
if(mHits[mHits.length-1]-mHits[0]<500){
//满足双击事件后,调用代码
int left = window_width/2 - iv_drag.getWidth()/2; //left表示控件左边缘距屏幕左侧的距离
int top = window_height/2 - iv_drag.getHeight()/2;
int right = window_width/2+iv_drag.getWidth()/2;
int bottom = window_height/2+iv_drag.getHeight()/2;
//控件按以上规则显示
iv_drag.layout(left, top, right, bottom);
//存储最终位置
SharePreferenceUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_X, iv_drag.getLeft());
SharePreferenceUtil.putInt(getApplicationContext(), ConstantValue.LOCATION_Y, iv_drag.getTop());
}
}
});
//监听某一个控件的拖拽事件(按下、移动、放下)
iv_drag.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX(); //获取点击位置(移动前)相对于整个屏幕原点(左上)的x距离
startY = (int) event.getRawY(); //获取点击位置(移动前)相对于整个屏幕原点(左上)的y距离
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getRawX(); //获取点击位置(移动后)相对于整个屏幕原点(左上)的x距离
int moveY = (int) event.getRawY();//获取点击位置(移动后)相对于整个屏幕原点(左上)的y距离
int disX=moveX-startX; //x方向的移动距离
int disY=moveY-startY; //y方向的移动距离
//1.当前控件所在屏幕的左上角的位置
//getLeft()获取该控件与父控件的相对位置
int left=iv_drag.getLeft()+disX; //控件(移动后)左侧坐标=控件(移动前)左侧坐标+移动距离
int top=iv_drag.getTop()+disY; //顶端坐标
int right=iv_drag.getRight()+disX;
int bottom=iv_drag.getBottom()+disY;
//容错处理:控件不能拖拽出手机屏幕
//左边缘不能超出屏幕
if(left<0){
return true;
}
if (right>window_width){
return true;
}
if (top<0){
return true;
}
if(bottom>window_height-30){
return true;
}
//2.告知移动的控件,按照计算出来的坐标去展示
iv_drag.layout(left,top,right,bottom);
//3.重置起始位置的坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
//4.存储移动后的位置坐标
SharePreferenceUtil.putInt(getApplicationContext(),ConstantValue.LOCATION_X,iv_drag.getLeft());
SharePreferenceUtil.putInt(getApplicationContext(),ConstantValue.LOCATION_Y,iv_drag.getTop());
break;
}
//(1)若只需要响应拖拽事件,应该返回true
//(2)既要响应点击事件,又要响应拖拽过程,则此返回值结果需要修改为false
return false;
}
});
}
}
3.效果图
Android Toast 自定义布局
在Toast工具类中添加一个子项,用来添加自定义布局的方法,直接上代码
/**
* toast 自定义
*/
public Toast showToastFree(Context ctx,String str,int resID){
toast = Toast.makeText(ctx, str, Toast.LENGTH_SHORT);
LinearLayout toastView = (LinearLayout)
LayoutInflater.from(ctx).inflate(R.layout.toast_hor_view, null);
ImageView iv = toastView.findViewById(R.id.toast_iv);
iv.setImageResource(resID);
TextView tv = toastView.findViewById(R.id.toast_tv);
tv.setText(str);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(toastView);
toast.show();
return toast;
}
布局 toast_hor_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="@drawable/shape_corner"
android:paddingEnd="12dp"
android:paddingStart="8dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:layout_height="50dp">
<ImageView
android:id="@+id/toast_iv"
android:layout_width="35dp"
android:layout_gravity="center_vertical"
android:layout_height="35dp" />
<TextView
android:id="@+id/toast_tv"
android:layout_marginLeft="10dp"
android:textColor="@color/white"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="00000"/>
</LinearLayout>
布局圆角背景 shape_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9703A9F4" />
<corners android:topLeftRadius="12dp"
android:topRightRadius="12dp"
android:bottomRightRadius="12dp"
android:bottomLeftRadius="12dp"/>
<stroke android:width="1dp" android:color="#000000" />
</shape>
android – 自定义Toast取消不起作用
我有一个定制的吐司.这个代码只有一行与http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView相同
区别如下.
> toast变量定义为类变量
>整个java代码都是用方法编写的.
>在这个方法的开头,我添加了以下行来取消吐司.
if (toast!=null){ toast.cancel(); }
当用户选择(onClick)视图/布局时,将调用该方法.问题是当用户选择几次时,toast将排队(toast.cancel不起作用).
有解决方案吗
[更新]
我尝试将toast对象设为静态变量.还是不行.
解决方法
将自定义Toast对象初始设置为null.
>如果为null,则使用“new”创建新的自定义Toast对象.
>就你在同一个活动中而言,不要“新”来创建新对象.相反,使用该对象.由于setText()在这种情况下不起作用,因此使用setView()就像使用自定义Toast一样.
>用这种方式show(),cancel(),show(),cancel()完全按照我的预期工作.没有延迟,没有排队.
希望这可以帮助.
Android 下自定义 Toast
自定义 Toast 中用到了 windowManager 这个类
一下为简单介绍:
应用程序与窗口管理器的接口。WindowManager 是 Android 中一个重要的服务。WindowManager Service 是全局唯一的。它将用户的操作,翻译成指令,发送呈现在界面上的各个 window。Activity 会将顶级控件注册到 Window Manager 中,当用户触碰屏幕或键盘时,Window Manager 就会通知到,而当控件有一些请求产生,也会经由 ViewParent 送回到 Window Manager 中,从而完成整个通信流程。
整个 Android 的窗口机制就是基于一个叫做 WindowManager,这个接口可添加 View 到屏幕,也可从屏幕移除 View。它面向的对象一端是屏幕,另一端就是 View,通过 WindowManager 的 addView () 创建 view,这样产生的 view 根据 WindowManager.LayoutParams 属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果。注意显示出来就一定要销毁掉 (remove ())。
三、获取其实例的方法
1、WindowManager wm=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
2、WindowManager wm=(WindowManager) getWindowManger();
下面直接贴代码
/**
* 自定义土司
*
* @param address
*/
public void myToast(String address) {
/*绘制UI界面*/
view = View.inflate(this, R.layout.address_show, null);
TextView textview = (TextView) view.findViewById(R.id.tv_address);
//"半透明","活力橙","卫士蓝","金属灰","苹果绿"
int[] ids = {R.mipmap.call_locate_white9, R.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9
, R.mipmap.call_locate_gray9, R.mipmap.call_locate_green9};
/*获得用户设置的风格,默认为0半透明*/
SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]);
textview.setText(address);
//窗体的参数就设置好了
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;
windowManager.addView(view, params);
}
Toast布局文件如下
<?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:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_call" />
<TextView
android:textColor="#000000"
android:id="@+id/tv_address"
android:textSize="22sp"
android:text="号码归属地"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
调用代码如下
package com.zaizai.safty.service;
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.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 com.zaizai.safty.R;
import com.zaizai.safty.db.dao.NumberAddressQueryUtils;
public class AddressService extends Service {
//窗体管理者
private WindowManager windowManager;
private View view;
/**
* 电话
*/
private TelephonyManager telephonyManager;
private MyListenerPhone myListenerPhone;
private OutCallReceiver outCallReceiver;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// 服务里面的内部类
//广播接收者的生命周期和服务一样
class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 这就是我们拿到的播出去的电话号码
String phone = getResultData();
// 查询数据库
String address = NumberAddressQueryUtils.queryNumber(phone);
// Toast.makeText(context, address, Toast.LENGTH_LONG).show();
myToast(address);
}
}
private class MyListenerPhone extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// state:状态,incomingNumber:来电号码
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:// 来电铃声响起
// 查询数据库的操作
String address = NumberAddressQueryUtils
.queryNumber(incomingNumber);
//Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG).show();
myToast(address);
break;
case TelephonyManager.CALL_STATE_IDLE://电话的空闲状态:挂电话、来电拒绝
//把这个View移除
if (view != null) {
windowManager.removeView(view);
}
break;
default:
break;
}
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//电话服务
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// 监听来电
myListenerPhone = new MyListenerPhone();
//注册服务去监听电话
telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_CALL_STATE);
//用代码去注册广播接收者Begin
outCallReceiver = new OutCallReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(outCallReceiver, filter);
//用代码去注册广播接收者End
//实例化窗体
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
}
/**
* 自定义土司
*
* @param address
*/
public void myToast(String address) {
/*绘制UI界面*/
view = View.inflate(this, R.layout.address_show, null);
TextView textview = (TextView) view.findViewById(R.id.tv_address);
//"半透明","活力橙","卫士蓝","金属灰","苹果绿"
int[] ids = {R.mipmap.call_locate_white9, R.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9
, R.mipmap.call_locate_gray9, R.mipmap.call_locate_green9};
/*获得用户设置的风格,默认为0半透明*/
SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]);
textview.setText(address);
//窗体的参数就设置好了
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;
windowManager.addView(view, params);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 取消监听来电
telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_NONE);
myListenerPhone = null;
//用代码取消注册广播接收者
unregisterReceiver(outCallReceiver);
outCallReceiver = null;
}
}
关于在Toast中自定义Android对讲和android 自定义toast的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于023 Android 自定义Toast控件、Android Toast 自定义布局、android – 自定义Toast取消不起作用、Android 下自定义 Toast的相关知识,请在本站寻找。
本文标签: