| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- * Copyright (C) 2015 pengjianbo([email protected]), Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package cn.finalteam.floatviewfinal.sample;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import cn.finalteam.floatviewfinal.service.FloatViewService;
- public class MainActivity extends AppCompatActivity {
- private FloatViewService mFloatViewService;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- Button btnShowFloat = (Button) findViewById(R.id.btn_show_float);
- btnShowFloat.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showFloatingView();
- }
- });
- Button btnHideFloat = (Button) findViewById(R.id.btn_hide_float);
- btnHideFloat.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- hideFloatingView();
- }
- });
- try {
- Intent intent = new Intent(this, FloatViewService.class);
- startService(intent);
- bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
- } catch (Exception e) {
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if ( keyCode == KeyEvent.KEYCODE_BACK ) {
- finish();
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- protected void onDestroy() {
- destroy();
- super.onDestroy();
- }
- /**
- * 显示悬浮图标
- */
- public void showFloatingView() {
- if ( mFloatViewService != null ) {
- mFloatViewService.showFloat();
- }
- }
- /**
- * 隐藏悬浮图标
- */
- public void hideFloatingView() {
- if ( mFloatViewService != null ) {
- mFloatViewService.hideFloat();
- }
- }
- /**
- * 释放PJSDK数据
- */
- public void destroy() {
- try {
- stopService(new Intent(this, FloatViewService.class));
- unbindService(mServiceConnection);
- } catch (Exception e) {
- }
- }
-
- /**
- * 连接到Service
- */
- private final ServiceConnection mServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
- mFloatViewService = ((FloatViewService.FloatViewServiceBinder) iBinder).getService();
- }
- @Override
- public void onServiceDisconnected(ComponentName componentName) {
- mFloatViewService = null;
- }
- };
- }
|