package android.dream.floating;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
public class FloatingService extends Service {
private static FloatingService mySelf = null;
private View mView = null;
private WindowManager mWindowManager = null;
private boolean isViewAdd = false;
private LayoutParams mLayoutParams = null;
private TextView mName = null;
private ImageView mPre = null;
private ImageView mStartStop = null;
private ImageView mNext = null;
private float mTouchStartX;
private float mTouchStartY;
private int statusBarHeight;
private Context mContext;
private ActivityManager mActivityManager;
private RunningServiceInfo mMusicService = null;
private boolean isCurLauncher = false;
private boolean isShowAll = false;
private boolean isEnd = false;
@Override
public void onCreate() {
super.onCreate();
mySelf = FloatingService.this;
mContext = this;
initFloating(this);
}
@Override
public void onDestroy() {
removeView();
super.onDestroy();
}
public static FloatingService getService(){
return mySelf;
}
private void initFloating(Context context){
mView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.floating, null);
mView.setVisibility(View.GONE);
mView.setBackgroundColor(0x88000000);
mName = (TextView)mView.findViewById(R.id.name);
mPre = (ImageView)mView.findViewById(R.id.pre);
mStartStop = (ImageView)mView.findViewById(R.id.start_stop);
mNext = (ImageView)mView.findViewById(R.id.next);
mPre.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
mPre.setBackgroundColor(0xff0000ff);
mContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.previous"));
}else if(event.getAction() == MotionEvent.ACTION_UP){
mPre.setBackgroundColor(0x88000000);
}
return true;
}
});
mStartStop.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
mStartStop.setBackgroundColor(0xff0000ff);
if(isPlaying()){
mContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.pause"));
mStartStop.setImageResource(R.drawable.zzzz_icon_play_normal);
}else{
mContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.togglepause"));
mStartStop.setImageResource(R.drawable.zzzz_icon_pause_normal);
}
}else if(event.getAction() == MotionEvent.ACTION_UP){
mStartStop.setBackgroundColor(0x88000000);
}
return true;
}
});
mNext.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
mNext.setBackgroundColor(0xff0000ff);
mContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.next"));
}else if(event.getAction() == MotionEvent.ACTION_UP){
mNext.setBackgroundColor(0x88000000);
}
return true;
}
});
mView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
refreshView((int)(event.getRawX() - mTouchStartX), (int)(event.getRawY() - mTouchStartY));
break;
case MotionEvent.ACTION_UP:
mTouchStartX = 0;
mTouchStartY = 0;
break;
}
return false;
}
});
mWindowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_SYSTEM_ERROR, LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSPARENT);
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
refreshView();
new CheckTopActivityTast().execute(0);
}
public boolean musicServiceIsRunning() {
List<RunningServiceInfo> infoList = null;
if(mActivityManager==null) {
mActivityManager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
}
infoList = mActivityManager.getRunningServices(100);
for(RunningServiceInfo info:infoList) {
if("com.android.music".equals(info.process)) {
mMusicService = info;
return true;
}
}
return false;
}
public boolean isPlaying() {
if(musicServiceIsRunning()) {
if (mMusicService != null) {
return mMusicService.foreground;
}
}
return false;
}
private void refreshView(int x, int y) {
if (statusBarHeight == 0) {
View rootView = mView.getRootView();
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
statusBarHeight = r.top;
}
mLayoutParams.x = x;
mLayoutParams.y = y - statusBarHeight;
refreshView();
}
private void refreshView() {
if (isViewAdd) {
mWindowManager.updateViewLayout(mView, mLayoutParams);
} else {
mWindowManager.addView(mView, mLayoutParams);
isViewAdd = true;
}
}
public void removeView() {
if (isViewAdd) {
mWindowManager.removeView(mView);
isViewAdd = false;
}
}
public void startFloating(){
if(isPlaying()){
mStartStop.setImageResource(R.drawable.zzzz_icon_pause_normal);
}else{;
mStartStop.setImageResource(R.drawable.zzzz_icon_play_normal);
}
isEnd = false;
showView();
}
public void endFloating(){
isEnd = true;
showView();
}
public void onlyFloating(){
isShowAll = false;
showView();
}
public void allFloating(){
isShowAll = true;
showView();
}
private void showView(){
if(isEnd){
mView.setVisibility(View.GONE);
}else{
if(isShowAll){
mView.setVisibility(View.VISIBLE);
}else{
if(isCurLauncher){
mView.setVisibility(View.VISIBLE);
}else{
mView.setVisibility(View.GONE);
}
}
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
if(msg.what == 1111){
showView();
}
}
};
class CheckTopActivityTast extends AsyncTask<Object, Object, String> {
@Override
protected String doInBackground(Object... arg0) {
// TODO Auto-generated method stub
String top_activity = null;
boolean islauncher = false;
while (true) {
top_activity = getTopActivityClassName();
Log.d("zhouzhiyuan","top_activity="+top_activity);
if(top_activity.contains("launcher")){
islauncher = true;
}else{
islauncher = false;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(isCurLauncher =
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
floating.rar (45个子文件)
floating
bin
classes.dex 14KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
zzzz_icon_pause_normal.png 1KB
ic_launcher.png 4KB
zzzz_icon_pre_normal.png 1KB
zzzz_icon_next_normal.png 1KB
zzzz_icon_play_normal.png 1KB
drawable-mdpi
ic_launcher.png 2KB
floating.apk 26KB
classes
android
dream
floating
R$layout.class 438B
R$id.class 594B
R$drawable.class 599B
FloatingActivity.class 2KB
FloatingService$2.class 1KB
FloatingService$4.class 1KB
R$attr.class 352B
FloatingService.class 8KB
R.class 559B
FloatingService$CheckTopActivityTast.class 2KB
R$string.class 550B
FloatingService$3.class 2KB
FloatingService$5.class 1KB
FloatingApplication.class 655B
FloatingActivity$1.class 1024B
FloatingService$1.class 804B
resources.ap_ 18KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
zzzz_icon_pause_normal.png 1KB
ic_launcher.png 4KB
zzzz_icon_pre_normal.png 4KB
zzzz_icon_next_normal.png 1KB
zzzz_icon_play_normal.png 1KB
values
strings.xml 377B
drawable-mdpi
ic_launcher.png 3KB
layout
floating.xml 1KB
main.xml 930B
assets
gen
android
dream
floating
R.java 2KB
proguard.cfg 1KB
src
android
dream
floating
FloatingService.java 8KB
FloatingApplication.java 274B
FloatingActivity.java 2KB
.project 811B
.classpath 356B
project.properties 361B
AndroidManifest.xml 1KB
共 45 条
- 1
~yportnE
- 粉丝: 34
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2022年各城市PM2.5, PM10, SO2, NO2等环境空气质量数据
- Golang:通过Gin框架+Redis+责任链,实现一个简单的钉钉机器人,进行消息处理 ps:多应用版
- 2021年各城市PM2.5, PM10, SO2, NO2等环境空气质量数据
- CORRUPT.navicat150-premium-cs-x64.exe
- centos7 ssh 升级至 9.6p1
- DriverMax Pro .exe
- PHP端通过modbus协议跟第三方设备进行数据通信
- navicat安装包亲测可用
- 算法部署-使用OpenVINO部署MobileStyleGAN轻量化高保真图像合成算法-项目源码-优质项目实战.zip
- 基于java实现远程采集华为逆变器使用modbus tcp协议进行通讯的设备数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页