Commit 0ccaee01 authored by Administrator's avatar Administrator

重播

parent 02f5bfc2
Pipeline #2434 canceled with stages
......@@ -3,12 +3,6 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.virjar.majora.adr">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 前台服务,保持安排的优先级 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".TheApp"
android:allowBackup="false"
......@@ -25,10 +19,18 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity" />
<service
android:name="com.virjar.majora.adr.KeepAliveService"
android:enabled="true"
android:exported="false" />
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 前台服务,保持安排的优先级 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
\ No newline at end of file
......@@ -22,10 +22,15 @@ import android.preference.PreferenceManager;
import com.virjar.majora.client.sdk.client.MajoraClient;
import com.virjar.majora.client.sdk.log.MajoraLogger;
import java.util.Timer;
import java.util.TimerTask;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
public class KeepAliveService extends Service {
private static boolean start = false;
// 重播任务定时器
private Timer timer = new Timer("reDial");
public static void startService(Context context) {
if (start) {
......@@ -89,10 +94,27 @@ public class KeepAliveService extends Service {
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
startForeground(110, notification);
startProxyService();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
reDial();
}
}, 5 * 60 * 1000, 5 * 60 * 1000);
start = true;
}
private void reDial() {
try {
Shell.executeCmd("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true");
Thread.sleep(5000);
Shell.executeCmd("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false");
} catch (Exception e) {
MajoraLogger.getLogger().info("reDial error", e);
}
}
private void startProxyService() {
SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);
String serverHost = spf.getString("server_host", "majora.virjar.com");
......
package com.virjar.majora.adr;
import com.virjar.majora.client.sdk.log.MajoraLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Shell {
public static void executeCmd(String cmd) {
try {
Process process = Runtime.getRuntime().exec("su");
OutputStream outputStream = process.getOutputStream();
outputStream.write(cmd.getBytes());
outputStream.write("\n".getBytes());
StringBuffer out = new StringBuffer();
StringBuffer error = new StringBuffer();
autoFillOutput(process.getInputStream(), out);
autoFillOutput(process.getErrorStream(), error);
process.waitFor();
} catch (Throwable throwable) {
MajoraLogger.getLogger().info("executeCmd error", throwable);
}
}
private static void autoFillOutput(final InputStream inputStream, final StringBuffer stringBuffer) {
new Thread() {
@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\n");
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment