MainActivity.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2015 pengjianbo([email protected]), Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cn.finalteam.floatviewfinal.sample;
  17. import android.content.ComponentName;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.content.ServiceConnection;
  21. import android.os.Bundle;
  22. import android.os.IBinder;
  23. import android.support.design.widget.FloatingActionButton;
  24. import android.support.design.widget.Snackbar;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.support.v7.widget.Toolbar;
  27. import android.view.KeyEvent;
  28. import android.view.Menu;
  29. import android.view.MenuItem;
  30. import android.view.View;
  31. import android.widget.Button;
  32. import cn.finalteam.floatviewfinal.service.FloatViewService;
  33. public class MainActivity extends AppCompatActivity {
  34. private FloatViewService mFloatViewService;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  40. setSupportActionBar(toolbar);
  41. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  42. fab.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  46. .setAction("Action", null).show();
  47. }
  48. });
  49. Button btnShowFloat = (Button) findViewById(R.id.btn_show_float);
  50. btnShowFloat.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View view) {
  53. showFloatingView();
  54. }
  55. });
  56. Button btnHideFloat = (Button) findViewById(R.id.btn_hide_float);
  57. btnHideFloat.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. hideFloatingView();
  61. }
  62. });
  63. try {
  64. Intent intent = new Intent(this, FloatViewService.class);
  65. startService(intent);
  66. bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
  67. } catch (Exception e) {
  68. }
  69. }
  70. @Override
  71. public boolean onCreateOptionsMenu(Menu menu) {
  72. // Inflate the menu; this adds items to the action bar if it is present.
  73. getMenuInflater().inflate(R.menu.menu_main, menu);
  74. return true;
  75. }
  76. @Override
  77. public boolean onOptionsItemSelected(MenuItem item) {
  78. // Handle action bar item clicks here. The action bar will
  79. // automatically handle clicks on the Home/Up button, so long
  80. // as you specify a parent activity in AndroidManifest.xml.
  81. int id = item.getItemId();
  82. //noinspection SimplifiableIfStatement
  83. if (id == R.id.action_settings) {
  84. return true;
  85. }
  86. return super.onOptionsItemSelected(item);
  87. }
  88. @Override
  89. public boolean onKeyDown(int keyCode, KeyEvent event) {
  90. if ( keyCode == KeyEvent.KEYCODE_BACK ) {
  91. finish();
  92. }
  93. return super.onKeyDown(keyCode, event);
  94. }
  95. @Override
  96. protected void onDestroy() {
  97. destroy();
  98. super.onDestroy();
  99. }
  100. /**
  101. * 显示悬浮图标
  102. */
  103. public void showFloatingView() {
  104. if ( mFloatViewService != null ) {
  105. mFloatViewService.showFloat();
  106. }
  107. }
  108. /**
  109. * 隐藏悬浮图标
  110. */
  111. public void hideFloatingView() {
  112. if ( mFloatViewService != null ) {
  113. mFloatViewService.hideFloat();
  114. }
  115. }
  116. /**
  117. * 释放PJSDK数据
  118. */
  119. public void destroy() {
  120. try {
  121. stopService(new Intent(this, FloatViewService.class));
  122. unbindService(mServiceConnection);
  123. } catch (Exception e) {
  124. }
  125. }
  126. /**
  127. * 连接到Service
  128. */
  129. private final ServiceConnection mServiceConnection = new ServiceConnection() {
  130. @Override
  131. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  132. mFloatViewService = ((FloatViewService.FloatViewServiceBinder) iBinder).getService();
  133. }
  134. @Override
  135. public void onServiceDisconnected(ComponentName componentName) {
  136. mFloatViewService = null;
  137. }
  138. };
  139. }