Skip to content

Commit

Permalink
加入辅助服务状态的广播
Browse files Browse the repository at this point in the history
  • Loading branch information
leon committed Jan 17, 2016
1 parent 16b5129 commit 028568d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/codeboy/qianghongbao/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
public class Config {

public static final String ACTION_QIANGHONGBAO_SERVICE_DISCONNECT = "com.codeboy.qianghongbao.ACCESSBILITY_DISCONNECT";
public static final String ACTION_QIANGHONGBAO_SERVICE_CONNECT = "com.codeboy.qianghongbao.ACCESSBILITY_CONNECT";

public static final String PREFERENCE_NAME = "config";
public static final String KEY_ENABLE_WECHAT = "KEY_ENABLE_WECHAT";
public static final String KEY_WECHAT_AFTER_OPEN_HONGBAO = "KEY_WECHAT_AFTER_OPEN_HONGBAO";
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/codeboy/qianghongbao/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.codeboy.qianghongbao;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -32,8 +36,31 @@ protected void onCreate(Bundle savedInstanceState) {
getFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commitAllowingStateLoss();

QHBApplication.activityStartMain(this);

IntentFilter filter = new IntentFilter();
filter.addAction(Config.ACTION_QIANGHONGBAO_SERVICE_CONNECT);
filter.addAction(Config.ACTION_QIANGHONGBAO_SERVICE_DISCONNECT);
registerReceiver(qhbConnectReceiver, filter);
}

private BroadcastReceiver qhbConnectReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(isFinishing()) {
return;
}
String action = intent.getAction();
Log.d("MainActivity", "receive-->" + action);
if(Config.ACTION_QIANGHONGBAO_SERVICE_CONNECT.equals(action)) {
if (mTipsDialog != null) {
mTipsDialog.dismiss();
}
} else if(Config.ACTION_QIANGHONGBAO_SERVICE_DISCONNECT.equals(action)) {
showOpenAccessibilityServiceDialog();
}
}
};

@Override
protected void onResume() {
super.onResume();
Expand All @@ -49,6 +76,9 @@ protected void onResume() {
@Override
protected void onDestroy() {
super.onDestroy();
try {
unregisterReceiver(qhbConnectReceiver);
} catch (Exception e) {}
mTipsDialog = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
Expand Down Expand Up @@ -61,6 +62,7 @@ public void onCreate() {
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "qianghongbao service destory");
if(mAccessbilityJobs != null && !mAccessbilityJobs.isEmpty()) {
for (AccessbilityJob job : mAccessbilityJobs) {
job.onStopJob();
Expand All @@ -69,17 +71,24 @@ public void onDestroy() {
}
service = null;
mAccessbilityJobs = null;
//发送广播,已经断开辅助服务
Intent intent = new Intent(Config.ACTION_QIANGHONGBAO_SERVICE_DISCONNECT);
sendBroadcast(intent);
}

@Override
public void onInterrupt() {
Log.d(TAG, "qianghongbao service interrupt");
Toast.makeText(this, "中断抢红包服务", Toast.LENGTH_SHORT).show();
}

@Override
protected void onServiceConnected() {
super.onServiceConnected();
service = this;
//发送广播,已经连接上了
Intent intent = new Intent(Config.ACTION_QIANGHONGBAO_SERVICE_CONNECT);
sendBroadcast(intent);
Toast.makeText(this, "已连接抢红包服务", Toast.LENGTH_SHORT).show();
}

Expand Down

0 comments on commit 028568d

Please sign in to comment.