Discussion Locked

How to run bat file in android app

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi Guys,

I am new to this process. Please let me know how to execute this below .bat file on mobile. It runs perfectly on the desktop through the task scheduler. I want the same execution on mobile.

@echo off

Title Start and Kill Internet Explorer

Mode con cols=100 lines=25 & color 2B
echo

Start "" "C:\Program Files\Google\Chrome\Application\chrome.exe" "https://google.com"

:: Sleep for 600 seconds, you can change the SleepTime variable

set SleepTime=600

Timeout /T %SleepTime% /NoBreak>NUL

Taskkill /IM "chrome.exe" /F

3 replies

by muhammadbilal.gn

Running a .bat file directly on an Android app is not straightforward since Android uses a different operating system (Linux) compared to Windows. However, you can achieve similar functionality by creating an Android app that performs equivalent tasks using a different approach, like Java or Python.

To create an Android app that opens a web page in a browser and then closes it after a specified time, you can use Java or Kotlin. Here's a simplified example in Java:

`java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Start the browser with the specified URL
String url = "https://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);

// Define a delay to close the browser
int sleepTime = 600000; // 600 seconds
new Handler(Looper.getMainLooper()).postDelayed(() -> {
// Close the browser
Intent closeIntent = new Intent(Intent.ACTION_MAIN);
closeIntent.addCategory(Intent.CATEGORY_HOME);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(closeIntent);
}, sleepTime);
}
}
`

Please note that this is just a simple example, and creating a full-fledged Android app may require more development work, including permissions, handling user interactions, and ensuring proper error handling.

Remember to adapt and customize the code to your specific requirements and Android development environment. Additionally, distributing apps with such functionality may have legal and ethical considerations, so make sure you are in compliance with app store policies and laws.

by apkwebsite3 edited

Running a batch file (.bat) directly in an Android app is not supported by default due to security and compatibility reasons. However, you can achieve similar functionality by executing shell commands or scripts within your Android app. Here's how you can do it using Java code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Run a shell command
try {
Process process = Runtime.getRuntime().exec("your_command_here");
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
reader.close();

// Now, 'output' contains the result of your command.
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the Runtime.getRuntime().exec() method, replace "your_command_here" with the shell command you want to execute. The process.getInputStream() retrieves the output of the command.

Also, remember to request the necessary permissions in your AndroidManifest.xml file if your command requires specific permissions, such as accessing the internet or reading external storage.

Please note that executing shell commands or scripts in your app should be done carefully, and you should be aware of the security implications. Only execute commands from trusted sources and sanitize any user input to prevent security vulnerabilities in your app.

This discussion is closed to new replies.