package

com.example.floatingdemo;

import java.util.Timer;

import java.util.TimerTask;

import android.app.Service;

import

android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import

android.content.IntentFilter;

import

android.graphics.PixelFormat;

import

android.media.AudioManager;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.view.Gravity;

import android.view.View;

import

android.view.WindowManager;

import

android.view.WindowManager.LayoutParams;

import

android.widget.ImageView;

import

android.widget.ProgressBar;

public class FloatingDemoService

extends Service {

WindowManager mWindowManager;

WindowManager.LayoutParams

mWindowParams;

AudioManager mAudioManager;

View mRootView;

ProgressBar progressBar_voice;

myReceiver receiver;

ImageView iv_voice_off;

ImageView iv_voice_on;

private Timer progressTimer;

private TimerTask progressTask;

Handler mHandler = new Handler()

{

@Override

public void handleMessage(Message

msg) {

super.handleMessage(msg);

VolumeAudioView();

int volume = msg.what;

progressBar_voice.setProgress(volume);

setBugle(volume);

}

};

@Override

public IBinder onBind(Intent arg0)

{

return null;

}

@Override

public void onCreate() {

super.onCreate();

createView();

receiver = new myReceiver();

IntentFilter filter = new

IntentFilter();

filter.addAction("com.ods.voice");

registerReceiver(receiver,

filter);

}

@Override

public int onStartCommand(Intent

intent, int flags, int startId) {

// TODO Auto-generated method

stub

return super.onStartCommand(intent,

flags, startId);

}

@Override

public void onDestroy() {

destoryView();

super.onDestroy();

}

private void createView() {

mAudioManager = (AudioManager)

getSystemService(Context.AUDIO_SERVICE);

mWindowManager = (WindowManager)

getSystemService(WINDOW_SERVICE);

mWindowParams = new

LayoutParams();

mWindowParams.type =

WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;

mWindowParams.flags |=

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

mWindowParams.gravity =

Gravity.CENTER_HORIZONTAL

| Gravity.CENTER_VERTICAL;

mWindowParams.x = 0;

mWindowParams.y = 0;

mWindowParams.width =

WindowManager.LayoutParams.FILL_PARENT;

mWindowParams.height =

WindowManager.LayoutParams.WRAP_CONTENT;

mWindowParams.format =

PixelFormat.RGBA_8888;

mRootView = View.inflate(this,

R.layout.voice, null);

//mRootView.setVisibility(View.INVISIBLE);

progressBar_voice = (ProgressBar)

mRootView

.findViewById(R.id.progressBar_voice);

iv_voice_off = (ImageView)

mRootView.findViewById(R.id.iv_voice_off);

iv_voice_on = (ImageView)

mRootView.findViewById(R.id.iv_voice_on);

int curentVolume =

mAudioManager

.getStreamVolume(AudioManager.STREAM_MUSIC);

setBugle(curentVolume);

progressBar_voice.setMax(mAudioManager

.getStreamMaxVolume(AudioManager.STREAM_MUSIC));

progressBar_voice.setProgress(curentVolume);

mWindowManager.addView(mRootView,

mWindowParams);

}

public void setBugle(int volume)

{

if (volume > 0) {

iv_voice_off.setVisibility(View.VISIBLE);

iv_voice_on.setVisibility(View.INVISIBLE);

} else {

iv_voice_off.setVisibility(View.INVISIBLE);

iv_voice_on.setVisibility(View.VISIBLE);

}

}

private void destoryView() {

mWindowManager.removeView(mRootView);

}

private class myReceiver extends

BroadcastReceiver {

@Override

public void onReceive(Context

context, Intent intent) {

int streamvolume =

intent.getIntExtra("streamvolume", 0);

Message message =

mHandler.obtainMessage(streamvolume);

mHandler.sendMessage(message);

}

}

public void VolumeAudioView() {

mRootView.setVisibility(View.VISIBLE);

if (progressTimer != null) {

progressTimer.cancel();

}

progressTimer = new Timer();

if (progressTask != null) {

progressTask.cancel();

}

progressTask = new TimerTask()

{

@Override

public void run() {

handler.sendEmptyMessage(0);

}

Handler handler = new Handler()

{

@Override

public void handleMessage(Message

msg) {

mRootView.setVisibility(View.INVISIBLE);

super.handleMessage(msg);

}

};

};

progressTimer.schedule(progressTask,

5000);

}

}

弹出框页面

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐