Can't kill SystemUI with activityManager.killBackgroundProcesses - android

first question here and I am pretty knew to Android development so excuse me.
For part of my app I would like to add a button that can restart the systemui, I am doing this by the following code.
#Override
public void onClick(View v) {
ActivityManager activityManager = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.android.systemui");
}
I have gave the application SU permission with the following code
public Process p;
{
try {
p = Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
}
Many thanks in advance.

You should have the permission KILL_BACKGROUND_PROCESSES to be able to call this method.
su doesn't play a role here, because you are using android's framework for this operation. If you use busybox to kill the process, than you need su.
Generally, it is not a good idea to kill SystemUI, though.

SystemUI cannot be killed by using Android framework's features. You should use Runtime.getRuntime().exec("su -c killall com.android.systemui"); if you want to kill SystemUI with root. Note that the sereen will go black until SystemUI is restarted soon.

Related

Turn off device programmatically

I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.
My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.
I do not have the shutdown command. I do have the reboot command.
reboot -p does not help. It simply freezes the device while remaining powered on.
The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:
PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());
I am open to all suggestions - hacky or otherwise. The version of Android is expected to remain at 4.2.2.
Intents
This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
The most luck I had with this was to request a shutdown by Intent:
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);
Shutdown Thread
That is a bit closer. Definitely interesting. Can you find an example of using it?
So far I have come up with this:
Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);
for (Method m : sdClass.getDeclaredMethods()) {
if (m.getName().matches("shutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("rebootOrShutdown")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
} else if (m.getName().matches("beginShutdownSequence")) {
m.setAccessible(true);
m.invoke(sdClass, PlayerActivity.this, false);
}
}
shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:
java.lang.UnsatisfiedLinkError: Native method not found:
com.android.server.power.PowerManagerService.nativeShutdown:()V at
com.android.server.power.PowerManagerService.nativeShutdown(Native
Method) at
com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)
at
com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)
at
com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)
lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.
In my case, I do not think it is possible to shut the device down how I would like to.
The closest that I managed to get to my target was using:
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);
That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.
In any case, I hope that my testing will help others in their quest.
It work for me on rooted device.
If your device is rooted then you can use below approach
Shutdown:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Restart:
Same code, just use "reboot" instead of "reboot -p".
Runtime.getRuntime().exec(new String[]{ "su", "-c", "reboot -p" });
it works, just with rooted devices!!
To use this code, you need Super User! Works on 4.0 and above!
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", false);
i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
and put this permission on manifest:
<uses-permission android:name="android.permission.SHUTDOWN" />
An update: for newer Android version, in my case is Android 8.1, they changed the action name. See below:
Intent i = new Intent("com.android.internal.intent.action.REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", false);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Good luck!
In newer android versions you aren't allowed to shut down the device from the nonSystem app.

Android: Child process (logcat) keeps running after parent process (app) died

our Android app spawns a logcat shell process and then reads its result for processing.
However, when the app stops (e.g. when restarted after recompilation during development), the logcat process will keep running. Here's an example of this behaviour:
processes = new ArrayList<Process>();
try {
processes.add(Runtime.getRuntime().exec("logcat -v time"));
processes.add(Runtime.getRuntime().exec("logcat -v time"));
processes.add(Runtime.getRuntime().exec("logcat -v time"));
processes.add(Runtime.getRuntime().exec("logcat -v time"));
} catch (IOException e) {
// oh no!
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
for (Process p : processes) {
p.destroy();
}
};
});
Add this to the onCreate() method of a test app, start it, then force-stop it using the settings manager. The child processes will keep running, now with a parent-id of 1.
In How to kill logcat process initiated by Android application? it was suggested to use ProcessBuilder, but that wasn't a solution and the process will keep running, too.
In Stop child process when parent process stops it was suggsted to use a shutdown hook - that doesn't work either as demonstrated above.
Thanks!
What you could do is spwaning another script of which the sole purpose is to watch your Java program. Whenever it dies, kill all of its children too.
A fragile example:
int pid = android.os.Process.myPid();
String script = "while [ -d /proc/" + pid + " ];do sleep 1;done; killall logcat";
Process p = Runtime.getRuntime().exec("/system/bin/sh", "-c", script);
This assumes that your process does not run as root, thereby only killing its own logcat processes. In your shutdown function, you should first kill the other processes (logcat) and then run p.destroy(); to stop this killer script.
The script above can be improved by removing the use of killall. Instead, get the process IDs of your logcat processes using Reflection (this answer points to http://www.golesny.de/p/code/javagetpid) and pass them to kill.

Error setting the CPU scaling governor through app

I'm creating a simple Android app that should set the CPU Scaling Governor.
To achieve this I made this function:
public static void setCurrentGovernor(String governor){
Process process;
try{
String cpufreq_path= "/sys/devices/system/cpu/cpu0/cpufreq";
String cmd = "echo `"+governor+"` > "+cpufreq_path+"/scaling_governor ";
process = Runtime.getRuntime().exec(new String[] {"su","-c",cmd});
} catch(IOException e){
e.printStackTrace();
}
}
My problem is that this function won't work, plus superuser always prompts me for permissions, is there a way to give it root permission only at first app boot?
This function is called inside an onitemselectedlistener of a spinner, right after this function there's another one which fetches data from cpufreq files to update the view, but if i do cat scaling_governor i get the old governor, so it is not a faulty update function.
This more solid code solved my problem:
Link

How to Programmatically press a button on Install/UnInstall APK Screen in android

Basically I want to install/uninstall an app on my android device from a remote Server.
I send a specific message (such as install or uninstall) from the remote server to my device.
But when the device initiates the process a system genrated Intent will start and shows the message below.
The OK button must be pressed in order to continue the process.
How can I programmatically press this button from the remote server and continue the process?
Hopefully you understand what I want to explain.
Any suggestions or ideas?
I'm afraid that this is possible only from play store. Click on the recycle bin, but not for external apps.
You can only ask the system to uninstall an app. Here's the reference.
Also, as pointed out in the comments:
When you open a dialog, the choice is user-driven.
It's against security guidelines.
I am looking the same solution for uninstalling any application by sending a SMS from server.
Bellow I'm giving some sample code it may help you.But you need your device as rooted one.
For rooting your device please download s/w from bellow link
http://forum.xda-developers.com/showthread.php?t=803682
The code is
public class MainActivity extends Activity {
Context context;
// boolean isEnabled;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uninstall();
}
private void Uninstall() {
Process process;
try {
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount,rw -t rfs /dev/stl5 /system; \n");
os.writeBytes("rm -r /system/app/ActionsContentViewExample.apk; \n");
os.writeBytes("mount -o remount,ro -t rfs /dev/stl5 /system; \n");
} catch (IOException e) {
e.printStackTrace();
}
}
}

How to detect when the user launches another app? (Android)

I'm trying to build an application where my application runs in the background and detects when the user launches another application so that I can control the flow from thereon.
To illustrate my query, I'd like to specify an example.
My application is running in the background (say as a Service), and the user has just clicked on application 'XYZ'. Is it possible for my app to detect that app 'XYZ' has been launched?
More than just detecting whether 'XYZ's Activity has come to the foreground,I want to detect whther 'XYZ' has been launched or not. Say someone launches 'Whatsapp Messenger', I want to know if my app can know that 'Whatsapp Messenger' has been launched.
EDIT : A lot of people think I'm trying to build malware, but I'm not. I'm trying to build an app for a high school project. I want a stat to see how often I use my camera as part of a psych project. :/
Thanks in advance,
Sumit.
Yes, You can find the which application is launched, by Tracking the Logcat. Just Track on ActivityManager tag with info -I log.
From adb shell Command is,
adb logcat ActivityManager:I *:S
From your application code,
logcat ActivityManager:I *:S
And in Logcat you can find a line something like,
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
When any application will launched.
It is logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":
Update:
Just add permission in your Application's manifest file,
android.permission.READ_LOGS
I guess you should have a look at "app protector" applications in the Google Play. They detect that user launched another application. That is done by reading system logs. Try opening LogCat and reading logs after you launched any application on device. You'll be surprised.
And where should you go from there? I guess you should try aLogCat app. It's freen and open-source. That will help you to actually read logs.
All this is considered to be a security breach in Android by some developers, though.
I have made a service which can detect if other application launches. I have made it for dialer. similarly that can be replaced by any package name.
#Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(this,"Service Started", Toast.LENGTH_LONG).show();
final String str = "";
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int phonelaunched = 0,phoneclosed =0;
int phonelaunches = 1;
#Override
public void run() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for ( ActivityManager.RunningAppProcessInfo appProcess: runningAppProcessInfo ) {
Log.d(appProcess.processName.toString(),"is running");
if (appProcess.processName.equals("com.android.dialer")) {
if ( appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND /*isForeground(getApplicationContext(),runningAppProcessInfo.get(i).processName)*/){
if (phonelaunched == 0 ){
phonelaunched = 1;
Log.d(str,"dude phone has been launched");
}
else if (phoneclosed == 1){
phonelaunches++;
phoneclosed = 0;
Log.d(String.valueOf(phonelaunches),"dude that was counter");
}
}
else {
phoneclosed = 1;
Log.d(str,"dude phone has been closed");
}
}
}
}
},2000,3000);
return START_STICKY;
}
Here I go through all the running tasks and check if it is our intended application. If so I check if the application is foreground and application is never launched using 'phonelaunched' variable. phoneclosed is used when intended application is in background and variable is set accordingly.
All this is implemented in Service class
In my book, by the way you posed the question, that sounds like hi-jacking an app in a certain way for your service to control, bordering on malware jinx. But it will not work in Android - plain and simple due to the permissions of each application is different. Thereby, each app are isolated from one another. So to answer your question bluntly, its No.
As the other answer suggested - you can monitor the logcat but.. then again... why?

Categories

Resources