-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from Jinnrry/feature-exit
添加退出程序按钮,代码整理
- Loading branch information
Showing
12 changed files
with
260 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
Android/app/src/main/java/cn/xjiangwei/RobotHelper/Broadcast/ButtonBroadcastReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
Android/app/src/main/java/cn/xjiangwei/RobotHelper/Service/Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
Oops, something went wrong.