监听文本变化
//文本监听器方法
number.addTextChangedListener(new TextWatcher() {
@Override
//当文本变化完成之后的时候调用
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//当文本变化完成的时候调用
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String phone = s.toString();
String address = AddressDao.queryAddress(phone, getApplication());
if (!TextUtils.isEmpty(address)) {
result.setText(address);
}else{
result.setText("");
}
}
//当文本变化完成之前的时候调用
@Override
public void afterTextChanged(Editable s) {
}
});
抖动和震动的效果
//抖动效果
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
number.startAnimation(shake);
//震动效果
Vibrator vibrator= (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(100);
return;
震动还需要加权限
<uses-permission android:name="android.permission.VIBRATE"/>
实现一个当有电话拨打进来的时候显示他的归属地
1.定义一个服务
package ligang.huse.cn.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import ligang.huse.cn.db.dao.AddressDao;
import ligang.huse.cn.mobilesafe.AToolsActivity;
public class AddressService extends Service {
private MyPhoneStateListener myPhoneStateListener;
private TelephonyManager telephonyManager;
public AddressService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
//获取电话管理者
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
myPhoneStateListener = new MyPhoneStateListener();
//实现对电话管理者的监听事件
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
super.onCreate();
}
private class MyPhoneStateListener extends PhoneStateListener {
@Override
//incomingNumber:表示来电号码
//state:表示来电的状态
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE://空闲状态
break;
case TelephonyManager.CALL_STATE_RINGING://响铃状态
String address = AddressDao.queryAddress(incomingNumber, getApplicationContext());
Toast.makeText(getApplicationContext(), "电话归属地:"+address, Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK://通话状态
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
@Override
public void onDestroy() {
//销毁服务
//PhoneStateListener.LISTEN_NONE:表示没有任何监听
telephonyManager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_NONE);
super.onDestroy();
}
}
2.在splash界面中开启这个服务
//开启来电显示号码的服务
Intent intent = new Intent(this, AddressService.class);
startService(intent);
实现是否开启来电显示电话号码的服务
//是否开启来电提示
private void update_address() {
if (AddressUtils.isRunning("ligang.huse.cn.service.AddressService", getApplicationContext())) {
tv_seeting_laidian.setBox(true);
} else {
tv_seeting_laidian.setBox(false);
}
tv_seeting_laidian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SettingActivity.this, AddressService.class);
if (tv_seeting_laidian.booleanBox()) {
//关闭提示更新
stopService(intent);
tv_seeting_laidian.setBox(false);
} else {
//开启提示更新
startService(intent);
tv_seeting_laidian.setBox(true);
}
}
});
}
动态获取服务是否开启
public static boolean isRunning(String className, Context context) {
//进程的管理者,活动的管理者
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//获取正在运行的服务
List<RunningServiceInfo> runningServices = manager.getRunningServices(1000);//1000,表示是获取到的最大的进程数
//遍历集合
for(RunningServiceInfo runningServiceInfo : runningServices) {
//获取控件的标示
ComponentName service = runningServiceInfo.service;
//获取正在运行的服务的全类名
String className2 = service.getClassName();
//将获取到的正在运行的服务的全类名和传递过来的服务的全类名比较,一直表示服务正在运行 返回true,不一致表示服务没有运行 返回false
if (className.equals(className2)) {
return true;
}
}
return false;
}
自定义吐司
//自定义吐司
private void showAddress(String address) {
//1.获取windowmanager
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
View view = View.inflate(getApplicationContext(), R.layout.toast_custom_layout, null);
TextView number = (TextView) view.findViewById(R.id.number);
number.setText(address);
//3.设置toast的属性
//layoutparams是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; // 执行toast的类型
//2.将view对象添加到windowManager中
//params : layoutparams 控件的属性
//将params属性设置给view对象,并添加到windowManager中
windowManager.addView(view, params);
}
移动改变吐司的样式
[1]定义一个自定义控件
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number"
android:text="中国大连"
android:textColor="@android:color/holo_blue_bright"
android:textSize="30sp"/>
</LinearLayout>
package ligang.huse.cn.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import ligang.huse.cn.mobilesafe.R;
/**
* 自定义控件
*/
public class SettingClick extends RelativeLayout {
private TextView title;
private TextView des;
private String des_on;
private String des_off;
private String tiltle2;
public SettingClick(Context context) {
super(context);
init();
}
public SettingClick(Context context, AttributeSet attrs) {
super(context, attrs);
tiltle2 = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "title2");
des_on = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "des_on");
des_off = attrs.getAttributeValue("http://schemas.android.com/ligang.huse.cn.mobilesafe", "des_off");
init();
}
public SettingClick(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
//第一种加载自定义布局的方式
/* View view = View.inflate(getContext(), R.layout.settingview, null);
this.addView(view);*/
//第二种加载自定义布局的方式
View view = View.inflate(getContext(), R.layout.settingclick, this);
title = (TextView) view.findViewById(R.id.tv_setting_title);
des = (TextView) view.findViewById(R.id.tv_setting_des);
title.setText(tiltle2);
}
/*设置标题内容*/
public void setTitle(String titles) {
title.setText(titles);
}
/*设置文本内容*/
public void setDes(String Des) {
des.setText(Des);
}
}
[2]使用自定义控件
package ligang.huse.cn.mobilesafe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class DragViewActivity extends AppCompatActivity {
private LinearLayout ll_dragView_toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_view);
ll_dragView_toast = (LinearLayout) findViewById(R.id.ll_dragView_toast);
changed();
}
//移动改变toast的样式
private void changed() {
ll_dragView_toast.setOnTouchListener(new View.OnTouchListener() {
private int startY;
private int startX;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int newX = (int) event.getRawX();
int newY = (int) event.getRawY();
int lX = newX - startX;
int ly = newY - startY;
int l = ll_dragView_toast.getTop();
int t = ll_dragView_toast.getLeft();
l += lX;
t += ly;
int r = l + ll_dragView_toast.getWidth();
int b = t + ll_dragView_toast.getHeight();
ll_dragView_toast.layout(l,t,r,b);
startX=newX;
startY=newY;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
}
手机卫士第七天笔记
最新推荐文章于 2019-06-02 17:59:20 发布