Is it possible to intercept outgoing emails from Android? I have tried with content provider by using ContentObserver but still no luck.
this is my code.
final String AUTHORITY_PLUS_MESSAGES = "content://gmail-ls/messages/";
String[] gmailAccounts = getGmailAccount(this.getApplicationContext());
String firstEmailAddress = gmailAccounts[0];
Handler handler = new Handler();
ContentResolver resolver = getContentResolver();
ContentObserver observer = new GmailObserver(handler, resolver, firstEmailAddress);
Uri uri = Uri.parse(AUTHORITY_PLUS_MESSAGES + firstEmailAddress + "/");
resolver.registerContentObserver(uri, Boolean.TRUE, observer);
class GmailObserver extends ContentObserver {
public void onChange(final boolean bSelfChange) {
super.onChange(bSelfChange);
Log.d(TAG, "***** onChange");
// here I am getting lots of calls but not sure how to deal with outgoing messages ..
}
}
Is it possible to intercept outgoing emails from Android?
No, sorry.
Related
I know this question is way old and may be duplicate, but I have a particular query based on it for which I am still trying to find the solution.
As far as I know, ContentObserver on URI content://sms will be triggered when ever there is change in data.
I have tried,
https://katharnavas.wordpress.com/2012/01/18/listening-for-outgoing-sms-or-send-sms-in-android/
Android : Catching Outgoing SMS using ContentObserver or receiver not working
and many more...
As per my understanding this can only be achieved by Content provider and there is no broadcast to handle this. Also content://sms/sent doesn't work at
contentResolver.registerContentObserver(Uri.parse("content://sms/sent"), true, observer);
Am receiving the onChange for each messaging data whether its SMS sent, received, deleted. So as per the documentation of android and other stackoverflow links I found using https://developer.android.com/reference/android/provider/Telephony.TextBasedSmsColumns.html#MESSAGE_TYPE_SENT as comparison method.
So my check goes as,
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
cur.moveToFirst();
String id = cur.getString(cur.getColumnIndex("_id"));
String protocol = cur.getString(cur.getColumnIndex("protocol"));
Log.d(TAG, "protocol = " + protocol);
int type = cur.getInt(cur.getColumnIndex("type"));
Log.d(TAG, "onChange: type = " + type);
// I have even tried with protocol == null check.
if (type == 2 && smsChecker(id)) {
Log.d(TAG, "Sent sms");
}
cur.close();
}
private boolean smsChecker(String smsId) {
boolean flagSMS = true;
if (smsId.equals(lastSmsId)) {
flagSMS = false;
}
else {
lastSmsId = smsId;
}
return flagSMS;
}
So my question here is, If I delete any messages or trigger a outgoing message I receive type 2 on my HTC phone - L OS (sometimes for incoming message too I see type==2).
How can I separate this onChange request, as I am particularly looking for SMS SENT change.
I would like to monitor/filter the websites that an user opens in Android.
I know how to retrieve the last visited URL (in Android default browser) using a ContentObserver on the browser history...
private static class BrowserObserver extends ContentObserver {
private static String lastVisitedURL = "";
private static String lastVisitedWebsite = "";
//Query values:
final String[] projection = new String[] { Browser.BookmarkColumns.URL }; // URLs
final String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // history item
final String sortOrder = Browser.BookmarkColumns.DATE; // the date the item was last visited
public BrowserObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange);
//Retrieve all the visited URLs:
final Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, projection, selection, null, sortOrder);
//Retrieve the last URL:
cursor.moveToLast();
final String url = cursor.getString(cursor.getColumnIndex(projection[0]));
//Close the cursor:
cursor.close();
if ( !url.equals(lastVisitedURL) ) { // to avoid information retrieval and/or refreshing...
lastVisitedURL = url;
//Debug:
Log.d(TAG, "URL Visited: " + url + "\n");
}
}
}
To register the ContentObserver I use:
browserObserver = new BrowserObserver(new Handler());
getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, browserObserver);
And to unregister it:
getContentResolver().unregisterContentObserver(browserObserver);
This works. However, in this way, I can analyze the URLs only after the browser has loaded them.
Now, is there a way to retrieve the URLs before the browser actually loads them in Android?
A solution which could help to create a Web Monitor, is to creation your own VPN service, so that you monitor all the device traffic. A good example of this is the project NetGuard.
https://github.com/M66B/NetGuard
Note that in some devices, the system will not pass through the VPN some applications (ex, in Samsung devices, the Samsung Web Browser is not forwarded through the system VPN, checked in S5 with Android 6.0).
Also your application should request the permission to be used as a VPN Service, but once the user gives this permission, it can monitor and filter most of the device network traffic.
I'm trying to make an simple application to intercept all SMS received from my telephone operator's e-mail service.
How it works: I have an e-mail from my telephone operator's that notifies me every time that an e-mail comes to the inbox with a SMS. The SMS comes that way:
You have a new e-mail from:email#domain.com See it now through internet! Visit http://m.iclaro.com.br. Subject: SUBJECT GOES HERE
This app that i'm trying to make have to intercept these SMS, retrieve the entire subject ("SUBJECT GOES HERE") and send a fake SMS from a number with only the subject on its contents.
What I've already done: intercept all these SMS from this e-mail service, retrieve the subject and fake a new SMS from a new number (I've choosen 3) just with the subject.
But now I have a problem: this new faked SMS doesn't show any notification.
Here goes the BroadcastReceiver:
public class SmsReceiver extends BroadcastReceiver
{
...
public void onReceive( Context context, Intent intent )
{
...
if(address.contains("1") && body.contains(replace))
{
body = body.substring(body.lastIndexOf(replace),body.length());
body = body.replace(replace, "");
address = "3";
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("address", address);
values.put("body", body);
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
}
}
}
I had also tried to:
if(address.contains("1") && body.contains(replace))
{
this.abortBroadcast();
and
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
and
this.clearAbortBroadcast();
contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
Is there anyway to re-notify the last received SMS? Any suggestions?
You will have to encode pdu after editing the received sms message. For that you can use java libraries like smslib etc. for encoding pdu.
public class SmsReceiver extends BroadcastReceiver
{
...
public void onReceive( Context context, Intent intent )
{
...
if(address.contains("1") && body.contains(replace))
{
body = body.substring(body.lastIndexOf(replace),body.length());
body = body.replace(replace, "");
address = "3";
//ContentResolver contentResolver = context.getContentResolver();
//ContentValues values = new ContentValues();
//values.put("address", address);
//values.put("body", body);
//contentResolver.insert(Uri.parse("content://sms/inbox"), values);
this.abortBroadcast();
//create new pdu from the edited data
byte[] pdu = .......;
intent.putSerializableExtra("pdus", pdu);
context.sendBroadcast(intent);
}
}
}
This question asked how to know if Android Talkback is active; that worked until Jelly Bean. Starting from Android 4.1, that steps no longer work, because the mentioned cursor is empty.
Having this said, I want to ask is if there is a way to do the same checking in Jelly Bean.
EDIT
I tried to search for TalkBack code and I found it here.
For checking if TalkBack is active, I am using the following code:
Intent screenReaderIntent = new Intent("android.accessibilityservice.AccessibilityService");
screenReaderIntent.addCategory("android.accessibilityservice.category.FEEDBACK_SPOKEN");
List<ResolveInfo> screenReaders = getPackageManager().queryIntentServices(screenReaderIntent, 0);
Cursor cursor = null;
ContentResolver cr = getContentResolver();
for (ResolveInfo screenReader : screenReaders) {
cursor = cr.query(Uri.parse("content://" + screenReader.serviceInfo.packageName
+ ".providers.StatusProvider"), null, null, null, null);
//here, cursor is not null, but calling cursor.moveToFirst() returns false, which means the cursor is empty
}
Having this said, if the cursor is empty, how do we know if TalkBack is running?
EDIT 2
Following #JoxTraex suggestions, I am now sending a broadcast to query whether or not TalkBack is enabled:
Intent i = new Intent();
i.setAction("com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND");
sendBroadcast(i);
Now how should I receive the response?
I tried adding the following to manifest, but my receiver does not receive any response:
<receiver android:name="my.package.MyBroadcastReceiver"
android:permission="com.google.android.marvin.talkback.PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK">
<intent-filter>
<action android:name="com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND" />
</intent-filter>
This can be achieved much easier by using AccessibilityManager.
AccessibilityManager am = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
boolean isAccessibilityEnabled = am.isEnabled();
boolean isExploreByTouchEnabled = am.isTouchExplorationEnabled();
You can check the enabled spoken accessibility servers
fun Context.isScreenReaderEnabled(): Boolean {
val accessibilityManager = getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
if (!accessibilityManager.isEnabled)
return false
val serviceInfoList = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN)
if (serviceInfoList.isNullOrEmpty())
return false
return true
}
While looking at TalkBackService.java, I found these code segments. These segments should provide some insight on how to query the status.
Code
/**
* {#link Intent} broadcast action for querying the state of TalkBack. </p>
* Note: Sending intent broadcast commands to TalkBack must be performed
* through {#link Context#sendBroadcast(Intent, String)}
*/
#Deprecated
// TODO(caseyburkhardt): Remove when we decide to no longer support intent broadcasts for
// querying the current state of TalkBack.
public static final String ACTION_QUERY_TALKBACK_ENABLED_COMMAND = "com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND";
/**
* Result that TalkBack is enabled.
*
* #see #ACTION_QUERY_TALKBACK_ENABLED_COMMAND
*/
public static final int RESULT_TALKBACK_ENABLED = 0x00000001;
/**
* Result that TalkBack is disabled.
*
* #see #ACTION_QUERY_TALKBACK_ENABLED_COMMAND
*/
public static final int RESULT_TALKBACK_DISABLED = 0x00000002;
/**
* Permission to send {#link Intent} broadcast commands to TalkBack.
*/
public static final String PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK = "com.google.android.marvin.talkback.PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK";
/**
* Tag for logging.
*/
private static final String LOG_TAG = "TalkBackService";
public static final String ACTION_QUERY_TALKBACK_ENABLED_COMMAND = "com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND";
..
} else if (ACTION_QUERY_TALKBACK_ENABLED_COMMAND.equals(intentAction)) {
// TODO(caseyburkhardt): Remove this block when we decide to no
// longer support
// intent broadcasts for determining the state of TalkBack in
// favor of the content
// provider method.
if (sInfrastructureInitialized) {
setResultCode(RESULT_TALKBACK_ENABLED);
} else {
setResultCode(RESULT_TALKBACK_DISABLED);
}
}
...
}
Explanation
You must send an Intent broadcast to the TalkBackService using the action of:
public static final String ACTION_QUERY_TALKBACK_ENABLED_COMMAND = "com.google.android.marvin.talkback.ACTION_QUERY_TALKBACK_ENABLED_COMMAND";
Then examine the contents of the Extras and process it accordingly.
ALSO insure that you have the right permission:
public static final String PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK = "com.google.android.marvin.talkback.PERMISSION_SEND_INTENT_BROADCAST_COMMANDS_TO_TALKBACK";
I'm not sure this is the best way of achieving what is proposed, but I managed to make this work by using the following code:
Intent screenReaderIntent = new Intent("android.accessibilityservice.AccessibilityService");
screenReaderIntent.addCategory("android.accessibilityservice.category.FEEDBACK_SPOKEN");
List<ResolveInfo> screenReaders = getPackageManager().queryIntentServices(screenReaderIntent, 0);
Cursor cursor = null;
int status = 0;
ContentResolver cr = getContentResolver();
List<String> runningServices = new ArrayList<String>();
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
runningServices.add(service.service.getPackageName());
}
for (ResolveInfo screenReader : screenReaders) {
cursor = cr.query(Uri.parse("content://" + screenReader.serviceInfo.packageName
+ ".providers.StatusProvider"), null, null, null, null);
if (cursor != null && cursor.moveToFirst()) { //this part works for Android <4.1
status = cursor.getInt(0);
cursor.close();
if (status == 1) {
//screen reader active!
} else {
//screen reader inactive
}
} else { //this part works for Android 4.1+
if (runningServices.contains(screenReader.serviceInfo.packageName)) {
//screen reader active!
} else {
//screen reader inactive
}
}
}
Probably this is not the best way but it is the only one I can think of that works in Jelly Bean and in previous Android versions
I was looking quite a bit on the forums for determining how many SMS messages I have that were not read. The code below seems to work good for received messages, but once I actually read the message the onChange of the ContentObserver is not being called.
So, on incoming SMS message the onChange is getting called and I get the correct number, but after reading the message it is not being called. The device is Samsung Note running Android 2.3.6 not rooted. Any help will be appreciated.
public class UnreadSMSContentObserver extends ContentObserver {
private static final String myTAG="PhoneInfoSMS";
private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
private ContentResolver mContentResolver;
private Handler mHandler;
public int unreadSMS;
private static final String TAG = "PhoneInfoSMS";
public UnreadSMSContentObserver(ContentResolver cr, Handler h) {
super(null);
mContentResolver = cr;
mHandler = h;
}
public int getUnreadSMS() {
if (mContentResolver != null) {
try {
Cursor c = mContentResolver.query(SMS_INBOX, null, "read = 0", null, null);
if (c != null) {
unreadSMS = c.getCount();
c.deactivate();
Log.d(TAG, unreadSMS + " unread SMS messages");
}
}
catch (Exception ex) {
Log.e("ERROR: " + ex.toString(), "");
}
}
return unreadSMS;
}
#Override
public void onChange(boolean selfChange) {
Log.d(myTAG, "onChange");
if (mContentResolver != null) {
getUnreadSMS();
mHandler.obtainMessage(PhoneInfoServer.CONTENTO_INFOCHANGED, PhoneInfoServer.CONTENTO_US, unreadSMS).sendToTarget();
Log.d(myTAG, "done");
}
}
}
In the manifest file I have the following permissions:
"android.permission.READ_CONTACTS"
"android.permission.READ_SMS"
"android.permission.GET_ACCOUNTS"
"android.permission.BATTERY_STATS"
"android.permission.BLUETOOTH"
"android.permission.BLUETOOTH_ADMIN"
"android.permission.BROADCAST_SMS"
"android.permission.RECEIVE_SMS"
"android.permission.BROADCAST_SMS"
"android.permission.RECEIVE_MMS"
The way I set it up on the calling service is:
in the onCreate of the service I do:
mContentResolver = this.getContentResolver();
mUnreadSMSContentObserver = new UnreadSMSContentObserver(mContentResolver, mContentObserversHandler);
in the onStartCommand of the service I do:
mContentResolver.registerContentObserver(Uri.parse("content://sms/"), true, mUnreadSMSContentObserver);
mContentResolver.registerContentObserver(Uri.parse("content://sms/inbox/"), true, mUnreadSMSContentObserver);
I tried only the content://sms/ and only content://sms/inbox/ and both... did not solve the problem.
P.S. Similar method for the missed calls works perfect!
The application+service is sending over Bluetooth the battery data, missed calls and unread SMS messages to an Arduino based device that displays it on an LED screen. Why? Two reasons: first, this is my first Android program made for fun. The second is I am not carrying the phone with me at home all the time, and it is typically in my home office. If I did not hear it ringing I will still be able to see the LED display telling me I had calls or SMS when passing by.
this solution has worked for me :
context.getContentResolver().
registerContentObserver(Uri.parse("content://mms-sms/"), true, smsObserver);