Skip to content

Commit

Permalink
Merge pull request #36 from Jinnrry/feature-exit
Browse files Browse the repository at this point in the history
添加退出程序按钮,代码整理
  • Loading branch information
Jinnrry authored Jan 12, 2021
2 parents 941bc2b + 8517179 commit e263df9
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
android:resource="@xml/accessibility_service_config" />
</service>
<service
android:name=".Service.RunTime"
android:name=".Service.Controller"
android:enabled="true"
android:exported="false" /> <!-- 是否是xposed模块,xposed根据这个来判断是否是模块 -->
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@
public class HttpServer extends NanoHTTPD {


public boolean runing;
private static volatile HttpServer instance;
private boolean runing = false;

public static HttpServer getInstance() {
if (instance == null) {
synchronized (HttpServer.class) {
if (instance == null) {
instance = new HttpServer();
}
}
}
return instance;
}

public boolean isRuning() {
return runing;
}


public HttpServer() {
private HttpServer() {
super("0.0.0.0", 1082);
try {
start(5000, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cn.xjiangwei.RobotHelper.Broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import java.lang.reflect.Method;

import cn.xjiangwei.RobotHelper.Accessibility.HttpServer;
import cn.xjiangwei.RobotHelper.MainApplication;
import cn.xjiangwei.RobotHelper.Service.RunTime;

import static cn.xjiangwei.RobotHelper.Service.Controller.ACTION_BUTTON;
import static cn.xjiangwei.RobotHelper.Service.Controller.EXIT;
import static cn.xjiangwei.RobotHelper.Service.Controller.INTENT_BUTTONID_TAG;
import static cn.xjiangwei.RobotHelper.Service.Controller.START;
import static cn.xjiangwei.RobotHelper.Service.Controller.END;
import static cn.xjiangwei.RobotHelper.Service.Controller.END_HTTPSERVER;
import static cn.xjiangwei.RobotHelper.Service.Controller.START_HTTPSERVER;


/**
* 广播监听按钮点击事件
*/
public class ButtonBroadcastReceiver extends BroadcastReceiver {

public void collapseStatusBar(Context context) {
try {
Object statusBarManager = context.getSystemService("statusbar");

Method collapse;
collapse = statusBarManager.getClass().getMethod("collapsePanels");

collapse.invoke(statusBarManager);
} catch (Exception localException) {
localException.printStackTrace();
}

}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_BUTTON)) {
//通过传递过来的ID判断按钮点击属性或者通过getResultCode()获得相应点击事件
int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
switch (buttonId) {
case START:
collapseStatusBar(context);
if (!RunTime.getInstance().isRunning()) {
RunTime.getInstance().start();
} else {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainApplication.getInstance(), "运行中!", Toast.LENGTH_LONG).show();
}
});
}
break;
case END:
collapseStatusBar(context);
RunTime.getInstance().stop();
break;
case EXIT:
System.exit(9);
break;
case START_HTTPSERVER:
HttpServer.getInstance().start();
cn.xjiangwei.RobotHelper.Tools.Toast.show("HttpServer Start!");

break;
case END_HTTPSERVER:
HttpServer.getInstance().stop();
cn.xjiangwei.RobotHelper.Tools.Toast.show("HttpServer Stop!");

break;
default:
System.out.println("defatult : " + buttonId);
break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.io.File;

import cn.xjiangwei.RobotHelper.Service.RunTime;
import cn.xjiangwei.RobotHelper.Service.Controller;
import cn.xjiangwei.RobotHelper.Tools.MLog;
import cn.xjiangwei.RobotHelper.Tools.ScreenCaptureUtilByMediaPro;
import cn.xjiangwei.RobotHelper.Tools.TessactOcr;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void start(View view) {
}
// 启动屏幕监控
ScreenCaptureUtilByMediaPro.init();
Intent intent = new Intent(this, RunTime.class);
Intent intent = new Intent(this, Controller.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import android.app.Application;

import cn.xjiangwei.RobotHelper.Accessibility.HttpServer;
import cn.xjiangwei.RobotHelper.Service.Accessibility;
import cn.xjiangwei.RobotHelper.Service.RunTime;
import cn.xjiangwei.RobotHelper.Service.Controller;


public class MainApplication extends Application {
Expand Down Expand Up @@ -41,6 +42,6 @@ public boolean checkAccessibilityService() {
}

public boolean checkHttpServer() {
return RunTime.httpServer != null && RunTime.httpServer.runing;
return HttpServer.getInstance().isRuning();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cn.xjiangwei.RobotHelper.Service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import cn.xjiangwei.RobotHelper.Broadcast.ButtonBroadcastReceiver;
import cn.xjiangwei.RobotHelper.R;


public class Controller extends Service {
public final static String INTENT_BUTTONID_TAG = "ButtonId";
public final static int START = 1;
public final static int END = 2;
public final static int START_HTTPSERVER = 3;
public final static int END_HTTPSERVER = 4;
public final static int EXIT = 5;
private static final String CHANNEL_ID = "cn.xjiangwei.RobotHelper.channel";
public final static String ACTION_BUTTON = "com.notification.intent.action.ButtonClick";



public Controller() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}


public int onStartCommand(Intent intent, int flags, int startId) {
//处理任务
return START_STICKY;
}


@Override
public void onCreate() {

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);


NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager == null)
return;

NotificationChannel channel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
channel = new NotificationChannel(CHANNEL_ID, "xxx", NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
}

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContent(remoteViews)
.setSmallIcon(getApplicationInfo().icon)
.setCategory(Notification.CATEGORY_SERVICE)
.setOngoing(true)
.build();


//注册广播
ButtonBroadcastReceiver receiver = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
this.registerReceiver(receiver, intentFilter);

//设置点击的事件
Intent startIntent = new Intent(ACTION_BUTTON);
startIntent.putExtra(INTENT_BUTTONID_TAG, START);
PendingIntent start = PendingIntent.getBroadcast(this, START, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn1, start);


Intent endIntent = new Intent(ACTION_BUTTON);
endIntent.putExtra(INTENT_BUTTONID_TAG, END);
PendingIntent end = PendingIntent.getBroadcast(this, END, endIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn2, end);


//设置点击的事件
Intent startHttpserverIntent = new Intent(ACTION_BUTTON);
startHttpserverIntent.putExtra(INTENT_BUTTONID_TAG, START_HTTPSERVER);
PendingIntent startHttpserver = PendingIntent.getBroadcast(this, START_HTTPSERVER, startHttpserverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.start_http, startHttpserver);


Intent endHttpServerIntent = new Intent(ACTION_BUTTON);
endHttpServerIntent.putExtra(INTENT_BUTTONID_TAG, END_HTTPSERVER);
PendingIntent endHttpServer = PendingIntent.getBroadcast(this, END_HTTPSERVER, endHttpServerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.end_http, endHttpServer);


Intent exitIntent = new Intent(ACTION_BUTTON);
exitIntent.putExtra(INTENT_BUTTONID_TAG, EXIT);
PendingIntent exit = PendingIntent.getBroadcast(this, EXIT, exitIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.exit, exit);


startForeground(101, notification);

}




}

Loading

0 comments on commit e263df9

Please sign in to comment.