Prevent Activity From Launching - android

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.

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.

ObjectAnimator: how to detect framerate?

I use ObjectAnimator and testing app on different devices. On some old slowly phones it animates too bad (no GPU, no memory etc), so I want to automatically switch user to second (lite) version of an app.
So how to detect what ObjectAnimator is slowly?
Is it have some framerate report? I found nothing.
Is Choreographer have same listener? it lists errors in catlog, but I want to get it in code.
01-22 05:55:15.775: INFO/Choreographer(3794): Skipped 33 frames! The application may be doing too much work on its main thread.
Note: I know how to parse catlog via system command, but it seems to me it is not a smart or fast solution.
Note2: I do not need to make things faster. I need to detect slow phone.
Ok, this solution works fine
It can be called from ObjAnimator's loop (listener - onRepeat, for example)
You can count events and make conclusion about speed of device.
p.s. it is not smart enought, but until nobody answered about native framerate report...
boolean getCatLog(){
boolean yes=false;
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output;
String lastLine = null;
while ((output = reader.readLine()) != null) {
lastLine = output;
}
if(lastLine!=null && lastLine.contains("Choreographer")){
Log.d(TAG,"yes!"); //do not remove!
yes=true;
}
reader.close();
process.waitFor();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return yes;
}

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

Rooted Android Background onTouchListener

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.

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.

Categories

Resources