Detecting toast messages - android

I don't think this is possible, as I haven't found anything in the SDK documentation (yet).
But I could do with knowing if its possible to write an application which logs Toast messages. Logging which application showed it and what the message displayed contained.
This is an entirely personal endeavour to create an app which can detect the toast messages. Because something on my phone is creating a toast saying "Sending..." about once per day, and for the life of me I can't track down the offending application (Service class). I thought it might be GMail or Evernote, but there toast messages for sending are slightly different. I'm going for building an app because 1) I don't know if LogCat would show anything, and 2) I don't want to keep my personal/dev phone plugged in to a PC all the time (as the "Sending..." message occurs so infrequently).

It's possible to catch Messages/Notifications with an Accessibility Service, have a look at that.
You can extend the class AccessibilityService and override the method onAccessibilityEvent() to implement something like this:
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
return; // event is not a notification
String sourcePackageName = (String) event.getPackageName();
Parcelable parcelable = event.getParcelableData();
if (parcelable instanceof Notification) {
// Statusbar Notification
}
else {
// something else, e.g. a Toast message
String log = "Message: " + event.getText().get(0)
+ " [Source: " + sourcePackageName + "]";
// write `log` to file...
}
}
Note: This didn't work for me on Android 2.2 as it doesn't seem to catch Toasts, but it worked on Android 4.0+.

Related

How to keep my Android app connected to the backend channel to receive updates and trigger events in device?

I am working on an Android project where my app is connected to user_channel in background and shows a notification when a new event occurs in the channel.
The Viewmodel code
#RequiresApi(Build.VERSION_CODES.O)
fun getNotifications(callback: (Payload) -> Unit) {
Coroutines.main {
repository.receiveText { msg ->
Log.d("NOTIFICATION", msg.toString())
callback(msg)
}
}
}
The Repository code
suspend fun receiveText(callback: (Payload) -> Unit) {
coroutineScope {
async {
sosChannel.on("notification") { message ->
callback(message.payload)
}
}
}
}
The Activity code
viewModel.getNotifications() {
runOnUiThread {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AlertNotification.alert(this, it["message"].toString() + "at " + it["latitude"] + " " + it["longitude"])
v.vibrate(VibrationEffect.createOneShot(1500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(1500);
}
Toast.makeText(
this,
it["message"].toString() + "at " + it["latitude"] + " " + it["longitude"],
Toast.LENGTH_LONG
).show()
}
}
Now when the app is in foreground, and my app is connected to channel , I can check for the event and it works fine
But when the app is killed , the app leaves the channel an how can I keep connected to the channel to receive notification?
I also learned that post Android Oreo background job is limited, despite Facebook and whatsapp like apps stay active in the backgrond ans show notifications, how do they do that - How can I proceed to achieve my goal , i.e. receiving notifications by staying connected to the channel in background an calling the Viewomdel method
any suggestion is greatly appreciated , thanks in advance!
Facebook and whatsapp like apps stay active in the backgrond ans show notifications, how do they do that
The most appropriate way to show a notification even when the app is killed or in the background is to implement push notifications. This is typically how messaging apps alert about new messages.
That being said, push notifications need to be supported on both sides (your app and the back-end you're listening to). To support it on Android take a look at that https://firebase.google.com/docs/cloud-messaging
Otherwise, if you really wanna keep your app alive in the background and keep subscribing and running code (not good for battery and data usage) you will need to do that in a foreground service with a persistent notification for the user (not good for UX). I would advise avoiding this solution as much as possible.
https://developer.android.com/guide/components/services

dronekit-android API addMavlinkObserver causes system crash

I am using dronekit-android packages that can successfully connect to PX4 board. However, I try to receive the mavlink message in a while 1 thread, the receive API addMavlinkObserver causes the system crash.
After the drone is connected, I simply call this function upon pressing a button and it crashes.
this.drone.addMavlinkObserver(new MavlinkObserver() {
#Override
public void onMavlinkMessageReceived(MavlinkMessageWrapper mavlinkMessageWrapper) {
//Log.d("Received Mavlinks:", mavlinkMessageWrapper.getMavLinkMessage().toString());
Toast.makeText(getApplicationContext(), "MAV receive " + mavlinkMessageWrapper.getMavLinkMessage().toString(),Toast.L‌​ENGTH_LONG).show();
}
})
Does anyone have any idea?

Turning notifications to text-to-speech when driving

I have an application that according to some events, changes a normal notification to text-to-speech in order to since sometimes the phone isn't available to users, and it'll be safer not to handle the phone.
For example, when you're driving, this is dangerous, so i want to turn the notifications to text-to-speech.
I've looked for a long time some explanation for turning text-to-speech when driving, but i can't find any reference for that no where i search.
For generating text-to-speech, i have this part, which works fine :
private TextToSpeech mTextToSpeech;
public void sayText(Context context, final String message) {
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
try {
if (mTextToSpeech != null && status == TextToSpeech.SUCCESS) {
mTextToSpeech.setLanguage(Locale.US);
mTextToSpeech.speak(message, TextToSpeech.QUEUE_ADD, null);
}
} catch (Exception ex) {
System.out.print("Error handling TextToSpeech GCM notification " + ex.getMessage());
}
}
});
}
But, i don't know how to check if i'm currently driving or not.
As Ashwin suggested, you can use Activity recognition Api, but there's a downside of that, the driving samples you'll receive, has a field of 'confidence' which isn't always accurate, so you'll have to do extra work(such as check locations to see if you actually moved) in order to fully know if the user moved.
You can use google's FenceApi which allows you to define a fence of actions such as driving, walking, running, etc. This api launched recently. If you want a sample for using it, you can use this answer.
You can pull this git project (everything free), which does exactly what you want : adds to the normal notification a text-to-speech when you're driving.
In order to know whether you are driving or not you can use Activity Recognition API
Here is a great tutorial that might help you out Tutorial and Source Code

Android executing SU commands in background?

I'm writing an android app that sets the max frequency, governor etc.. when the screen turns off. To do it i have a service running that receives screen on/off broadcast intents. When the screen off event fires, I read from the shared preferences and set whatever is set by the user with this function
public static void writeFile(String file, String content) {
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(suProcess.getOutputStream());
out.writeBytes("echo '" + content + "' > '" + file + "'");
out.flush();
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
Then when the screen turns back on, i revert the system to its previous state by rewriting the files. Everything is working nicely, working as intended.
The problem is i'm noticing some lag when turning the screen on/off. Also when i turn the screen on, i see the toast message from SuperUser "App was granted su access", and it pops up once for every command. Is there a way to hide that toast message? I haven't found any way to hide a toast message from another activity. I know they can be disabled in the superuser app, but that's not ideal. I read you can write your own superuser binary but that sounds alot more complicated than simple java programming... also sounds like it could lead to security problems.
Basically i'm asking what's the best way to do this, so that it's as non-invasive to the user as possible?
I haven't used su, but I think if you don't close the stream every time it should only display toast once.

Sending alert from bluegiga dongle to android handset

I am trying to develop a BLE bluetooth (SMART) application for Android.I am using the Broadcom-ble API
I have a requirement to send alerts from Bluegiga dongle to the android handset. I send alert values such as 0,1,2 from bluegiga. Whenever I send the alert, the following callback is invoked inside my application:
#Override
public void onCharacteristicWrite(String address, BleCharacteristic charObj) {
Log.d(TAG, "onCharacteristicWrite(" + address + ", " + charObj + ")");
}
The following line of code gives nullpointer, when I try to fetch the alert value from the charObj
byte alertLevel = charObj.getValue()[0];
I have checked and confirmed, charObj is not null. But charObj.getValue() returns null.
Can someone please let me know, though the callback is invoked correctly when I send an alert from bluegiga, why I am unable to retrieve the sent value, from my application?
Any help is appreciated.

Categories

Resources