博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 仿ios屏幕右边的圆圈
阅读量:6427 次
发布时间:2019-06-23

本文共 7109 字,大约阅读时间需要 23 分钟。

public class TopFloatService extends Service implements OnClickListener,OnKeyListener{
WindowManager wm = null;
WindowManager.LayoutParams ballWmParams = null;
private View ballView;
private View menuView;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private RelativeLayout menuLayout;
private Button floatImage;
private PopupWindow pop;
private RelativeLayout menuTop;
private boolean ismoving = false;
private TextView btn_home_screen;
private TextView btn_apps;
private TextView btn_lock_screen;
private TextView btn_setting;
private TextView btn_favor;
/**
 * 屏幕锁屏
 */
private DevicePolicyManager policyManager;
private ComponentName componentName;
@Override
public void onCreate() {
super.onCreate();
 //获取设备管理服务
policyManager=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName=new ComponentName(getApplicationContext(),LockReceiver.class);
//加载辅助球布局
ballView = LayoutInflater.from(this).inflate(R.layout.floatball, null);
floatImage = (Button)ballView.findViewById(R.id.float_image);
setUpFloatMenuView();
createView();

}
/**
 * 窗口菜单初始化
 */
private void setUpFloatMenuView(){
menuView = LayoutInflater.from(this).inflate(R.layout.floatmenu, null);
menuLayout = (RelativeLayout)menuView.findViewById(R.id.menu);
menuTop = (RelativeLayout)menuView.findViewById(R.id.lay_main);
menuLayout.setOnClickListener(this);
menuLayout.setOnKeyListener(this);
menuTop.setOnClickListener(this);
//初始化
btn_apps=(TextView)menuView.findViewById(R.id.btn_apps);
btn_favor=(TextView)menuView.findViewById(R.id.btn_favor);
btn_home_screen=(TextView)menuView.findViewById(R.id.btn_home_screen);
btn_lock_screen=(TextView)menuView.findViewById(R.id.btn_lock_screen);
btn_setting=(TextView)menuView.findViewById(R.id.btn_setting);
//添加点击事件
btn_apps.setOnClickListener(this);
btn_favor.setOnClickListener(this);
btn_home_screen.setOnClickListener(this);
btn_lock_screen.setOnClickListener(this);
btn_setting.setOnClickListener(this);
}

/**
 * 通过MyApplication创建view,并初始化显示参数
 */
private void createView() {
wm = (WindowManager) getApplicationContext().getSystemService("window");
ballWmParams =  ((MyApplication) getApplication()).getMywmParams();
ballWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
ballWmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
ballWmParams.gravity = Gravity.LEFT | Gravity.TOP;
ballWmParams.x = 0;
ballWmParams.y = 0;
ballWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.format = PixelFormat.RGBA_8888;
//添加显示层
wm.addView(ballView, ballWmParams);
//注册触碰事件监听器
floatImage.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
x = event.getRawX();
y = event.getRawY(); 
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
ismoving = false;
mTouchStartX = (int)event.getX();
mTouchStartY = (int)event.getY();
break;
case MotionEvent.ACTION_MOVE:
ismoving = true;
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
mTouchStartX = mTouchStartY = 0;
break;
}
//如果拖动则返回false,否则返回true
if(ismoving == false){
return false;
}else{
return true;
}
}

});
//注册点击事件监听器
floatImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DisplayMetrics dm = getResources().getDisplayMetrics();
pop = new PopupWindow(menuView, dm.widthPixels, dm.heightPixels);
pop.showAtLocation(ballView, Gravity.CENTER, 0, 0);
pop.update();
}
});
}
/**
 * 更新view的显示位置
 */
private void updateViewPosition() {
ballWmParams.x = (int) (x - mTouchStartX);
ballWmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(ballView, ballWmParams);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_apps:
Intent intent= new Intent("android.intent.action.DIAL");  
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setClassName("com.android.contacts","com.android.contacts.DialtactsActivity");     
startActivity(intent);
break;
        
case R.id.btn_favor:
break;

case R.id.btn_home_screen:
Intent mHomeIntent=new Intent(Intent.ACTION_MAIN);
mHomeIntent.addCategory(Intent.CATEGORY_HOME);
mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(mHomeIntent);
break;

case R.id.btn_lock_screen:
lock();
android.os.Process.killProcess(android.os.Process.myPid());
break;

case R.id.btn_setting:
Intent mSetting=new Intent(Intent.ACTION_MAIN);
mSetting.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(mSetting);
// cm=new ComponentName("com.android.settings","com.android.settings.Settings");
//
Intent mSetting=new Intent(Settings.ACTION_APN_SETTINGS);
//
mSetting.setComponent(cm);
     
        startActivity(mSetting);
break;
default:
if(pop!=null && pop.isShowing()){
pop.dismiss();
}
break;
}
}
/**
 * 锁屏
 */
  public void lock(){
  boolean active=policyManager.isAdminActive(componentName);
  if(!active){  //若无权限  
  activeManager();  //去获得权限  
  policyManager.lockNow();   //并锁屏  
  }
  if(active){
  policyManager.lockNow();  //直接锁屏 
  }
  }
  
  /**
   * 获得权限
   */
  public void activeManager(){
      //使用隐式意图调用系统方法来激活指定的设备管理器 
  Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  //权限列表
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
  intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一键锁屏");
   startActivity(intent);
   
  }

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Toast.makeText(getApplicationContext(), "keyCode:"+keyCode, 1000).show();
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
pop.dismiss();
break;
case KeyEvent.KEYCODE_BACK:
pop.dismiss();
default:
break;
}
return true;
}
}

页面代码:
  <RelativeLayout
            android:id="@+id/lay_main"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="4.0px"
            android:visibility="visible" >

            <TextView
                android:id="@+id/btn_apps"
                style="@style/Icon"
                android:layout_centerInParent="true"
                android:drawableTop="@drawable/selector_ic_apps"
                android:text="@string/apps" />

            <TextView
                android:id="@+id/btn_home_screen"
                style="@style/Icon"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:drawableTop="@drawable/selector_ic_home"
                android:text="@string/home_screen" />

            <TextView
                android:id="@+id/btn_setting"
                style="@style/Icon"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:drawableTop="@drawable/selector_ic_phone"
                android:text="@string/setting" />

            <TextView
                android:id="@+id/btn_lock_screen"
                style="@style/Icon"
                android:layout_centerHorizontal="true"
                android:drawableTop="@drawable/selector_ic_power_down"
                android:text="@string/lock_screen" />

            <TextView
                android:id="@+id/btn_favor"
                style="@style/Icon"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:drawableTop="@drawable/selector_ic_star"
                android:text="@string/favor" />
        </RelativeLayout>

源码下载:

转载地址:http://kybga.baihongyu.com/

你可能感兴趣的文章
一例HP ADG数据恢复成功(8×73GB SCSI)
查看>>
虚拟化系列-Citrix XenServer 6.1 XenMotion与HA
查看>>
TFS创建团队项目(三)
查看>>
对发展的一点小感想
查看>>
示例化讲解RIP路由更新机制
查看>>
eclipse不能自动编译工程的解决方法
查看>>
Powershell管理系列(九)删除Exchange用户邮箱中多余的电子邮件地址
查看>>
Swt/Jface进度条
查看>>
.NET建议使用的大小写命名原则
查看>>
Git:错误:error:src refspec master does not match any
查看>>
SSIS 数据类型和类型转换
查看>>
Oracle数据库“Specified cast is农田valid”
查看>>
数据层新思路,写数据库无关的数据层 ORM在数据库内做更为合适
查看>>
armv8(aarch64)linux内核中flush_dcache_all函数详细分析【转】
查看>>
房地产英语 Real estate词汇
查看>>
python接口自动化测试(八)-unittest-生成测试报告
查看>>
第 26 章 MySQL
查看>>
C#中三种截屏方式总结
查看>>
Spring.net 学习笔记之ASP.NET底层架构
查看>>
C# System.Windows.Forms.WebBrowser中判断浏览器内核和版本
查看>>