Rooted Android Background onTouchListener - android

I'm wonder if there is any way (on rooted phone) to use onTouch method from background, do some think and then dispatch this touch to foreground application.

Create a process and throw this at it: getevent
Multiple new lines will come in every time the screen is touched. Must have root since it contains sensitive touch position information.
Something like this:
try {
Process process = Runtime.getRuntime().exec("su getevent"); //su to get root access
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
//A new line came in. So a touch event came in.
}
}
catch (IOException e) {}
Note: I haven't tested it but it should work. Maybe minor tweaks are necessary.

Related

Android: Logcat string for Bluetooth ON

My application will switch on Bluetooth. I want to wait till bluetooth is switched on.
I will look for string
MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1
in logcat and then proceed.
I want to know if this method is correct or i should be looking for some other string. What is the best way to know whether i am looking for right string in logcat. Is there any collection/document to learn what all info can be gathered using logcat
You have to be careful with below as logcat may prevent your app from responding. You should either run this piece of code or your own app in a seperate thread to keep it responsive. Below code asks logcat to send logs to your app and you can do investigate the logs as you see fit.
private static final String SEARCH_STRING = "MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1";
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
boolean didFind = false;
while ((line = bufferedReader.readLine()) != null && !didFind) {
log.append(line);
didFind = line.toUpperCase().contains(SEARCH_STRING))
}
}
catch (IOException e) {}
I hope it helps.

How to collect LogCat messages in an Android application

I have an application and I'd like to collect the LogCat messages of a specified level and tag.
Can I somehow get the accumulated messages at some point? I don't want to collect the messages one by one, it should be the sum of them like when I use adb to read the actual log. Is this possible?
Try this: Note that in Android 4 you will only see the log messages that were written by your own app unless you have root access.
public static String getLog(Context c) {
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
return log.toString();
} catch (IOException e) {
return null;
}
}
Why not just write them to a file instead? LogCat is really for real-time logs. There are lots of good quality logging packages that can log to a file if that's what you want to do.
Just as an example:
How to write logs in text file when using java.util.logging.Logger

What is the best way to view large text files from websites?

I tried webview.loadurl("www.blahblah.com/blah.txt") and it crashed after a couple of seconds. What else can I do?
I only want to view these .txt files from the web as fast and as stable as possible.
Also I came across asynctask while researching. What is it and can it help me with this?
Also how can I make them to a string if I want to display these text files as a listview item?
Asynctask is wrapper for a new thread that allow you to perform network tasks such as downloading. If you try to download on the UI thread, you will get a crash.
For the text file, download it to disc first, and then read it into memory and display it
StringBuilder txtString = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String textLine;
while ((textLine = br.readLine()) != null) {
txtString.append(line);
txtString.append('\n');
}
}
catch (IOException e) {
//error stuff
}

Is it possible for an Android application to see its own log file?

I tried to use LogCat on my Android device (which is using Android 1.5, and no, it's not possible to update it, not yet anyway) and found that it only tracks log events that happen while it's running.
Is it possible for an application to keep track of all events that are logged during its runtime and save them to a file?
(even the ones not triggered by itself)
Create a database and a IntentService that can write to it.
Start this service using an Intent whenever any component wants to log anything.
Am I missing something?
As it turns out, this tutorial describes a method for dumping a copy of the system logs to a StringBuilder object.
Code as copied from the tutorial:
public class LogTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) { }
}
}
Note that you'll need to include a permission for reading logs:
<uses-permission android:name="android.permission.READ_LOGS" />.
The important part of the code is the Runtime.getRuntime().exec("logcat -d") part. The -d switch is the part that tosses out the current log and quits. If I'm reading the documentation correctly, leaving that switch off should, in theory, leave logcat running, dumping successive lines of the log to its stdout as they show up.

Prevent Activity From Launching

Is there a good way of preventing an activity from launching? I'd like to build either a whitelist or blacklist of applications, then prevent those instances from being started.
One potential solution is to poll the running tasks every so often and shut them down, but this seems like it could eat through a lot of battery.
Sorry, this is not supported with the SDK.
Ok so this doesnt count as a "GOOD WAY", could very well be as draining if not more so on your battery, but in line with your polling method:
you could create a service that monitors the log cat (http://www.helloandroid.com/tutorials/reading-logs-programatically)
need permission in manifest:
<uses-permission android:name="android.permission.READ_LOGS" />
then in your service something like (very crude):
private void readLogAndKill(){
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("Starting Activity")){
if(line.contains("com.twitter.android"){
//kill twitter for example
}
}
}
} catch (IOException e) {
}
}
there may be someway to catch the "Starting Activity" events - or filter the log as you read it for just those msgs.

Categories

Resources