version:1.2
fix: update:完善快捷控制,监听home键
This commit is contained in:
@@ -93,6 +93,8 @@ public class ControlActivity extends AppCompatActivity {
|
||||
ConstraintLayout cl_battery;
|
||||
@BindView(R.id.cl_flashlight)
|
||||
ConstraintLayout cl_flashlight;
|
||||
@BindView(R.id.cl_location)
|
||||
ConstraintLayout cl_location;
|
||||
|
||||
|
||||
private ContentResolver crv;
|
||||
@@ -518,8 +520,10 @@ public class ControlActivity extends AppCompatActivity {
|
||||
private void getFlashlight() {
|
||||
if (isFlashlightEnabled()) {
|
||||
tv_flashlight_switch.setText("开");
|
||||
cl_flashlight.setBackground(getDrawable(R.drawable.control_background_item));
|
||||
} else {
|
||||
tv_flashlight_switch.setText("关");
|
||||
cl_flashlight.setBackground(getDrawable(R.drawable.control_background_item_dis));
|
||||
}
|
||||
cl_flashlight.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -528,10 +532,12 @@ public class ControlActivity extends AppCompatActivity {
|
||||
Settings.Secure.putInt(crv, Settings.Secure.FLASHLIGHT_ENABLED, 0);
|
||||
sendBroadcast(new Intent(ACTION_FLASHLIGHT_CHANGED));
|
||||
tv_flashlight_switch.setText("关");
|
||||
cl_flashlight.setBackground(getDrawable(R.drawable.control_background_item_dis));
|
||||
} else {
|
||||
Settings.Secure.putInt(crv, Settings.Secure.FLASHLIGHT_ENABLED, 1);
|
||||
sendBroadcast(new Intent(ACTION_FLASHLIGHT_CHANGED));
|
||||
tv_flashlight_switch.setText("开");
|
||||
cl_flashlight.setBackground(getDrawable(R.drawable.control_background_item));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -602,6 +608,8 @@ public class ControlActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private LocationClient locationClient;
|
||||
|
||||
private void getLocation() {
|
||||
BDLocation bdLocation = AmapManager.getInstance().getNowMapLocation();
|
||||
if (bdLocation != null) {
|
||||
@@ -615,7 +623,19 @@ public class ControlActivity extends AppCompatActivity {
|
||||
} else {
|
||||
tv_location.setText("定位失败");
|
||||
}
|
||||
LocationClient locationClient = AmapManager.getInstance().getLocationClient();
|
||||
startLocation();
|
||||
cl_location.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
startLocation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startLocation() {
|
||||
if (locationClient == null) {
|
||||
locationClient = AmapManager.getInstance().getLocationClient();
|
||||
}
|
||||
locationClient.stop();
|
||||
locationClient.start();
|
||||
locationClient.registerLocationListener(new BDAbstractLocationListener() {
|
||||
|
||||
@@ -38,6 +38,7 @@ import com.uiuios.aios.fragment.SecondFragment;
|
||||
import com.uiuios.aios.service.NotificationService;
|
||||
import com.uiuios.aios.utils.ApkUtils;
|
||||
import com.uiuios.aios.utils.AppUsedTimeUtils;
|
||||
import com.uiuios.aios.utils.HomeWatcher;
|
||||
import com.uiuios.aios.utils.ToastUtil;
|
||||
import com.uiuios.aios.view.ScaleCircleNavigator;
|
||||
|
||||
@@ -52,7 +53,7 @@ import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class MainActivity extends BaseActivity implements MainContact.MainView {
|
||||
private static String TAG = MainActivity.class.getSimpleName();
|
||||
private static final String TAG = MainActivity.class.getSimpleName();
|
||||
private MainPresenter mMainPresenter;
|
||||
|
||||
@BindView(R.id.viewPager)
|
||||
@@ -71,6 +72,8 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
|
||||
private int appListIndex = 1;
|
||||
private int defaultCurrent = 0;
|
||||
|
||||
private HomeWatcher mHomeWatcher;
|
||||
|
||||
@Override
|
||||
public int getLayoutId() {
|
||||
return R.layout.activity_main;
|
||||
@@ -141,6 +144,22 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
|
||||
if (mFragments.size() > 1) {
|
||||
mViewPager.setCurrentItem(defaultCurrent);
|
||||
}
|
||||
mHomeWatcher = new HomeWatcher(this);
|
||||
mHomeWatcher.setOnHomePressedListener(new HomeWatcher.OnHomePressedListener() {
|
||||
@Override
|
||||
public void onHomePressed() {
|
||||
//按了HOME键
|
||||
Log.e(TAG, "onHomePressed: ");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHomeLongPressed() {
|
||||
Log.e(TAG, "onHomeLongPressed: ");
|
||||
//长按HOME键
|
||||
}
|
||||
});
|
||||
mHomeWatcher.startWatch();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -346,6 +365,7 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onRestart() {
|
||||
super.onRestart();
|
||||
@@ -370,6 +390,9 @@ public class MainActivity extends BaseActivity implements MainContact.MainView {
|
||||
if (mNewAppReceiver != null) {
|
||||
unregisterReceiver(mNewAppReceiver);
|
||||
}
|
||||
if (mHomeWatcher != null) {
|
||||
mHomeWatcher.stopWatch();// 在销毁时停止监听,不然会报错的。
|
||||
}
|
||||
}
|
||||
|
||||
private void registmNewAppReceiver() {
|
||||
|
||||
86
app/src/main/java/com/uiuios/aios/utils/HomeWatcher.java
Normal file
86
app/src/main/java/com/uiuios/aios/utils/HomeWatcher.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.uiuios.aios.utils;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
|
||||
public class HomeWatcher{
|
||||
|
||||
private static final String TAG = "HomeWatcher";
|
||||
private Context mContext;
|
||||
private IntentFilter mFilter;
|
||||
private OnHomePressedListener mListener;
|
||||
private InnerRecevier mRecevier;
|
||||
|
||||
// 回调接口
|
||||
public interface OnHomePressedListener {
|
||||
public void onHomePressed();
|
||||
public void onHomeLongPressed();
|
||||
}
|
||||
|
||||
public HomeWatcher(Context context) {
|
||||
mContext = context;
|
||||
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置监听
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnHomePressedListener(OnHomePressedListener listener) {
|
||||
mListener = listener;
|
||||
mRecevier = new InnerRecevier();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监听,注册广播
|
||||
*/
|
||||
public void startWatch() {
|
||||
if (mRecevier != null) {
|
||||
mContext.registerReceiver(mRecevier, mFilter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止监听,注销广播
|
||||
*/
|
||||
public void stopWatch() {
|
||||
if (mRecevier != null) {
|
||||
mContext.unregisterReceiver(mRecevier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播接收者
|
||||
*/
|
||||
class InnerRecevier extends BroadcastReceiver {
|
||||
final String SYSTEM_DIALOG_REASON_KEY = "reason";
|
||||
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
|
||||
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
|
||||
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent){
|
||||
String action = intent.getAction();
|
||||
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
|
||||
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
|
||||
if (reason != null) {
|
||||
LogUtils.i("action:" + action + ",reason:" + reason);
|
||||
if (mListener != null){
|
||||
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)){
|
||||
// 短按home键
|
||||
mListener.onHomePressed();
|
||||
}else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)){
|
||||
// 长按home键
|
||||
mListener.onHomeLongPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user