Browse Source

add RestartByIntent Function, resolve restart App problem by device without root promision

dev
赵明涛 12 months ago
parent
commit
ba50ce9d29
  1. 6
      app/build.gradle
  2. 1
      app/src/main/AndroidManifest.xml
  3. 3
      daemonlibrary/src/main/AndroidManifest.xml
  4. 2
      daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/FFmpegShell.java
  5. 12
      daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/MainWorkService.java
  6. 49
      daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/RestartApp.java
  7. 2
      settings.gradle
  8. 1
      uniplugin_padprinter/src/main/AndroidManifest.xml
  9. 43
      uniplugin_padprinter/src/main/java/net/zmt/padprinter/Helper/Printer_Helper.java
  10. 69
      uniplugin_padprinter/src/main/java/net/zmt/padprinter/Helper/XTUtils.java
  11. 2
      uniplugin_padprinter/src/main/java/net/zmt/padprinter/UniPluginPadPrinter_AppProxy.java
  12. 4
      uniplugin_richalert/src/main/java/uni/dcloud/io/uniplugin_richalert/WatchDog/FFmpegShell.java
  13. 3
      uniplugin_richalert/src/main/java/uni/dcloud/io/uniplugin_richalert/WatchDog/RestartApp.java

6
app/build.gradle

@ -8,8 +8,8 @@ android {
minSdkVersion 21
targetSdkVersion 28 //21 io.dcloud.PandoraEntry apk入口时 targetSDKVersion>=21
versionCode 147
versionName "1.4.7"
versionCode 100
versionName "1.0.0"
multiDexEnabled true
ndk {
// abiFilters 'x86','armeabi-v7a'
@ -105,7 +105,7 @@ dependencies {
implementation project(':uniplugin_padprinter') //Pad Printer
// implementation project(':uniplugin_v4_ph')
// implementation project(':daemonlibrary')
implementation project(':daemonlibrary')
// implementation project(path: ':uniplugin_module_nanohttpd')

1
app/src/main/AndroidManifest.xml

@ -95,6 +95,7 @@
android:theme="@style/TranslucentTheme"
android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale"
android:hardwareAccelerated="true"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<data android:scheme="hbuilder"/>

3
daemonlibrary/src/main/AndroidManifest.xml

@ -25,6 +25,7 @@
>
<receiver
android:name="com.shihoo.daemon.watch.WakeUpReceiver"
android:exported="true"
android:process=":watch">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
@ -39,6 +40,7 @@
<receiver
android:name="com.shihoo.daemon.watch.WakeUpReceiver$StartWatchReceiver"
android:exported="true"
android:process=":watch">
<intent-filter>
<action android:name="com.shihoo.START_JOB_ALARM_SUB"/>
@ -47,6 +49,7 @@
<!-- 广播接收者 receiver 进程-->
<receiver
android:name="com.shihoo.daemon.watch.WakeUpReceiver$WakeUpAutoStartReceiver"
android:exported="true"
android:process=":watch">
<!-- 手机启动 -->
<intent-filter>

2
daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/FFmpegShell.java

@ -133,7 +133,7 @@ public class FFmpegShell {
public String execShell (String cmd) {
// StringBuilder s = new StringBuilder ();
// String[] command = cmd.split (" ");
Log.e(TAG, "execShell: 555555" +cmd);
Log.e(TAG, "execShell: 5555556-" +cmd);
ShellUtils.CommandResult result = ShellUtils.execCmd(cmd, true, false);
Log.e(TAG, result.result+" - execShell666: "+result.successMsg +" "+result.errorMsg );

12
daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/MainWorkService.java

@ -78,7 +78,7 @@ public class MainWorkService extends AbsWorkService {
public void startWork() {
Log.e(TAG, "检查磁盘中是否有上次销毁时保存的数据");
mDisposable = Observable
.interval(2, TimeUnit.SECONDS)
.interval(8, TimeUnit.SECONDS)
//取消任务时取消定时唤醒
.doOnDispose(new Action() {
@Override
@ -91,7 +91,8 @@ public class MainWorkService extends AbsWorkService {
@Override
public void accept(Long aLong) throws Exception {
Context context = getApplicationContext();
Boolean isRunning = restartApp.isAppRunning(context);
// Boolean isRunning = restartApp.isAppRunning(context);
Boolean isRunning = restartApp.isAppOnForeground(context);
//Log.d("wsh-daemon", "每 5 秒检测一次... count = " + aLong+" isRunning:"+isRunning.toString());
if (aLong > 0 && aLong % 18 == 0){
@ -99,9 +100,12 @@ public class MainWorkService extends AbsWorkService {
Log.d(TAG, " 计数 saveCount = " + (aLong / 18 - 1));
}
Log.d(TAG, "isRunning: "+isRunning);
if (!isRunning&&isNeedRestartApp){
Log.d(TAG, "App not in Top need do something");
restartApp.restartUniApp();
Log.d(TAG, "App not in Top need do something-daemonlibrary");
// restartApp.restartUniApp(); //因设备未root,无法获取su权限
// restartApp.restartAlarm(context);
restartApp.resetByIntent(context);
}else{
}

49
daemonlibrary/src/main/java/com/shihoo/daemon/watchdog/RestartApp.java

@ -11,8 +11,26 @@ import java.util.List;
public class RestartApp {
String TAG = "--RestartApp--";
public static Boolean isNeedRestartApp = true;
public static Boolean isNeedRestartApp = true;
// 发送intent,启动UNI应用
public void resetByIntent(Context context){
//创建一个Intent对象指定要启动的Activity的类名
// Intent intent = new Intent(this, io.dcloud.PandoraEntryActivity.class);
//
// ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// List<ActivityManager.RunningTaskInfo> list = activityManager.getRunningTasks(1);
// list.get(0).topActivity.getClassName().equals("io.dcloud.PandoraEntryActivity");
// Intent intent2 = new Intent(this, context.getApplicationContext().getClass());
Intent intent3 = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
Log.d(TAG, "resetByIntent: "+intent3.getPackage()+" "+intent3.toString());
//设置Intent的标志位为FLAG_ACTIVITY_NEW_TASK表示在新的任务栈中启动Activity
intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//调用startActivity方法传入Intent对象
context.startActivity(intent3);
}
public void restartByClearTop(Context context)
{
@ -58,9 +76,13 @@ public class RestartApp {
public void restartUniApp() {
if (isNeedRestartApp) {
FFmpegShell fFmpegShell = new FFmpegShell();
// am start
// adb shell
String cmd = "am start -n net.ouxuan.checkoutpad/io.dcloud.PandoraEntry -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"+"\n";
fFmpegShell.execRootShell(cmd);
String cmd = "adb shell am start -n net.ouxuan.checkoutpad/io.dcloud.PandoraEntry -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"+"\n";
// fFmpegShell.execRootShell(cmd);
String cmd2 ="chmod 755 adb shell am start -n net.ouxuan.checkoutpad/io.dcloud.PandoraEntry";
fFmpegShell.execShell(cmd);
// fFmpegShell.execRootShell(cmd);
}else{
Log.d(TAG, "restartUniApp: 不需要重启");
}
@ -96,4 +118,23 @@ public class RestartApp {
}
}
//判断当前app是否在前台运行
public boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = context.getPackageName();
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if(appProcesses == null) {
return false;
}
for(ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if(appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}
}

2
settings.gradle

@ -4,6 +4,6 @@ include ':uniplugin_component'
include ':uniplugin_richalert'
//include ':uniplugin_v4_ph'
//include ':daemonlibrary'
include ':daemonlibrary'
//include ':uniplugin_module_nanohttpd'
include ':uniplugin_padprinter'

1
uniplugin_padprinter/src/main/AndroidManifest.xml

@ -2,4 +2,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.zmt.padprinter">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

43
uniplugin_padprinter/src/main/java/net/zmt/padprinter/Helper/Printer_Helper.java

@ -21,6 +21,7 @@ import com.printer.sdk.PrinterConstants;
import com.printer.sdk.PrinterInstance;
import com.printer.sdk.PrinterConstants.Command;
import com.printer.sdk.PrinterConstants.Connect;
import com.printer.sdk.Table;
import com.printer.sdk.usb.USBPort;
import com.printer.sdk.utils.PrefUtils;
import com.printer.sdk.utils.XLog;
@ -79,13 +80,47 @@ public class Printer_Helper {
new Thread(new Runnable() {
public void run() {
// this.getResources()
XTUtils.printNote(application.getResources(), myPrinter);
// XTUtils.printNote(application.getResources(), myPrinter);
// XTUtils.printTable1(application.getResources(), myPrinter);
printOXTable(myPrinter);
}
}).start();
}
/**
*
* @Description: 打印需求定制表格小票 TODO:跟后台约定打印数据格式后改写
* @param
* @return void
*/
public static void printOXTable(PrinterInstance mPrinter){
//居中打印标题,完成后切回默认字体
mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_CENTER);
mPrinter.setFont(0, 1, 1, 0, 0);
mPrinter.printText("零售小票" + "\n");
mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_LEFT);
mPrinter.setFont(0, 0, 0, 0, 0);
String column = "品名;数量;单价;金额";
Table table = null;
table = new Table(column, ";", new int[] { 12, 6, 7, 7 });
table.addRow("-------------------------------\n");
table.addRow("" + "篮球(plus金牌教练服务+无限饮水)" + ";1;999.00;999.00");
table.addRow("" + "足球" + ";5;200.00;1000.00");
table.addRow("" + "乒乓球" + ";5;3.99;19.95");
table.addRow("-------------------------------\n");
table.addRow("金额合计: "+"11028.95"+"\n");
table.addRow("交易时间: "+"2024-01-17 13:37:24"+"\n");
table.addRow("订单号: "+"1213213123123212233"+"\n");
table.addRow("门店: "+"Test门店"+"\n");
mPrinter.printTable(table);
// mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 打印并走纸2行
mPrinter.cutPaper(65, 50); //65/66-半切/全切
}
@SuppressLint({"InlinedApi", "NewApi", "WrongConstant"})
public void usbAutoConn(UsbManager manager) {
// Context mContext = context;
@ -97,7 +132,7 @@ public class Printer_Helper {
}
mUSBDevice = deviceList.get(0);
if (mUSBDevice == null) {//处理连接失败
// mHandler.obtainMessage(PrinterConstants.Connect.FAILED).sendToTarget();
mHandler.obtainMessage(Connect.FAILED).sendToTarget();
return;
}
myPrinter = PrinterInstance.getPrinterInstance(mContext, mUSBDevice, mHandler);
@ -134,7 +169,7 @@ public class Printer_Helper {
&& mUSBDevice.equals(device)) {
myPrinter.openConnection();
} else {
// mHandler.obtainMessage(PrinterConstants.Connect.FAILED).sendToTarget();
mHandler.obtainMessage(Connect.FAILED).sendToTarget();
Log.e(TAG, "permission denied for device " + device);
}
}

69
uniplugin_padprinter/src/main/java/net/zmt/padprinter/Helper/XTUtils.java

@ -144,6 +144,12 @@ public class XTUtils {
return s;
}
/**
*
* @Description: 打印商店购物小票
* @param
* @return String
*/
public synchronized static void printNote(Resources resources, PrinterInstance mPrinter) {
mPrinter.initPrinter();
mPrinter.setFont(0, 0, 0, 0, 0);
@ -216,65 +222,79 @@ public class XTUtils {
}
/**
*
* @Description: 打印表格小票
* @param
* @return void
*/
public static void printTable1(Resources resources, PrinterInstance mPrinter) {
String column = resources.getString(R.string.note_title);
Table table = null;
if (PrinterConstants.paperWidth == 384) {
table = new Table(column, ";", new int[] { 14, 6, 6, 6 });
if (PrinterConstants.paperWidth == 384) { //
table = new Table(column, ";", new int[] { 14, 6, 6, 6 }); // 根据打印纸宽度设置表格宽度
} else if (PrinterConstants.paperWidth == 576) {
table = new Table(column, ";", new int[] { 18, 10, 10, 12 });
} else if (PrinterConstants.paperWidth == 724) {
table = new Table(column, ";", new int[] { 22, 14, 14, 18 });
}
table = new Table(column, ";", new int[] { 14, 6, 6, 6 });
table.addRow("" + resources.getString(R.string.bags) + ";10.00;1;10.00");
table.addRow("" + resources.getString(R.string.hook) + ";5.00;2;10.00");
table.addRow("" + resources.getString(R.string.umbrella) + ";5.00;3;15.00");
mPrinter.printTable(table);
}
/**
*
* @Description: 打印测试小票
* @param
* @return void
*/
public static synchronized void printTest(Resources resources, PrinterInstance mPrinter) {
mPrinter.initPrinter();
mPrinter.printText(resources.getString(R.string.str_text));
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 默认打印,并走纸2行
mPrinter.setFont(0, 0, 0, 0, 0);
mPrinter.setPrinter(Command.ALIGN, 0);
mPrinter.setFont(0, 0, 0, 0, 0); // 设置为默认字体
mPrinter.setPrinter(Command.ALIGN, 0); // 左对齐
mPrinter.printText(resources.getString(R.string.str_text_left));
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 2
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 文字居左打印并走纸2行
mPrinter.setPrinter(Command.ALIGN, 1);
mPrinter.setPrinter(Command.ALIGN, 1); // 居中对齐
mPrinter.printText(resources.getString(R.string.str_text_center));
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 2
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 文字居中打印并走纸2行
mPrinter.setPrinter(Command.ALIGN, 2);
mPrinter.setPrinter(Command.ALIGN, 2); // 右对齐
mPrinter.printText(resources.getString(R.string.str_text_right));
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3); // 3
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3); // 文字居右打印并走纸3行
mPrinter.setPrinter(Command.ALIGN, 0);
mPrinter.setFont(0, 0, 0, 1, 0);
mPrinter.setPrinter(Command.ALIGN, 0); // 左对齐
mPrinter.setFont(0, 0, 0, 1, 0); // 加粗
mPrinter.printText(resources.getString(R.string.str_text_strong));
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 2
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 加粗打印并走纸2行
mPrinter.setFont(0, 0, 0, 0, 1);
mPrinter.sendBytesData(new byte[] { (byte) 0x1C, (byte) 0x21, (byte) 0x80 });
mPrinter.setFont(0, 0, 0, 0, 1); // 加下划线
mPrinter.sendBytesData(new byte[] { (byte) 0x1C, (byte) 0x21, (byte) 0x80 }); //
mPrinter.printText(resources.getString(R.string.str_text_underline));
mPrinter.sendBytesData(new byte[] { (byte) 0x1C, (byte) 0x21, (byte) 0x00 });
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 2
mPrinter.sendBytesData(new byte[] { (byte) 0x1C, (byte) 0x21, (byte) 0x00 }); //
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2); // 加下划线打印并走纸2行
mPrinter.setFont(0, 0, 0, 0, 0);
mPrinter.setFont(0, 0, 0, 0, 0); // 默认字体
mPrinter.printText(resources.getString(R.string.str_text_height));
for (int i = 0; i < 4; i++) {
mPrinter.setFont(0, i, i, 0, 0);
for (int i = 0; i < 4; i++) { //放大倍数打印, 不走纸
mPrinter.setFont(0, i, i, 0, 0); // 宽度,高度放大倍数
mPrinter.printText((i + 1) + resources.getString(R.string.times));
}
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1); // 打印并走纸1行
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3); // 打印并走纸3行
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 4; i++) {//放大倍数打印后走纸3行
mPrinter.setFont(0, i, i, 0, 0);
mPrinter.printText(resources.getString(R.string.bigger) + (i + 1) + resources.getString(R.string.bigger1));
@ -283,7 +303,7 @@ public class XTUtils {
}
mPrinter.setFont(0, 0, 0, 0, 0);
mPrinter.setPrinter(Command.ALIGN, 0);
mPrinter.setPrinter(Command.ALIGN, 0); // 左对齐
mPrinter.setPrinter(Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);
}
@ -383,6 +403,7 @@ public class XTUtils {
}
// 2
@SuppressLint("LongLogTag")
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {

2
uniplugin_padprinter/src/main/java/net/zmt/padprinter/UniPluginPadPrinter_AppProxy.java

@ -21,7 +21,7 @@ public class UniPluginPadPrinter_AppProxy implements UniAppHookProxy {
public void initTest(Application application){
System.out.println("UniPluginPadPrinter_AppProxy initTest");
new Printer_Helper(application).TestPrinter(application);
// new Printer_Helper(application).TestPrinter(application); //启动时自动连接打印机并测试打印
}

4
uniplugin_richalert/src/main/java/uni/dcloud/io/uniplugin_richalert/WatchDog/FFmpegShell.java

@ -133,8 +133,8 @@ public class FFmpegShell {
public String execShell (String cmd) {
// StringBuilder s = new StringBuilder ();
// String[] command = cmd.split (" ");
Log.e(TAG, "execShell: 555555" +cmd);
ShellUtils.CommandResult result = ShellUtils.execCmd(cmd, true, false);
Log.e(TAG, "execShell: 555555: " +cmd);
ShellUtils.CommandResult result = ShellUtils.execCmd(cmd, false, false);
Log.e(TAG, result.result+" - execShell666: "+result.successMsg +" "+result.errorMsg );

3
uniplugin_richalert/src/main/java/uni/dcloud/io/uniplugin_richalert/WatchDog/RestartApp.java

@ -56,7 +56,8 @@ public class RestartApp {
FFmpegShell fFmpegShell = new FFmpegShell();
// adb shell
String cmd = "am start -n net.ouxuan.checkoutpad/io.dcloud.PandoraEntry -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"+"\n";
fFmpegShell.execRootShell(cmd);
// fFmpegShell.execRootShell(cmd);
fFmpegShell.execShell(cmd);
}

Loading…
Cancel
Save