I am trying to extract some data from an android notification.
When I receive a mail, I can get the name of the sender from the title attribute and the rest of the notification is in the text attribute with the following code:
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
if (sbn.getPackageName().equals("com.google.android.gm") {
String title = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE);
String text = "";
text = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();
Log.i(TAG, "Title: " + title);
Log.i(TAG, "Text: " + text);
}
Given now the following screenshot of a notification.
Android mail notification
The title is no longer the name of the sender (but 6 new messages) so I thought to take the text below and extract the name, but my code from above doesn't work because of an NullPointerException in line of text = sbn.getNotification()...
What am I missing or doing wrong? Does anyone know how to get the whole text or even a nice method to get the senders name?
Cheers
Related
I am using the following code to change the phone number of an outgoing call under a certain condition. The code seems to be working. The toast appears with the old and new number but the phone dialer shows the old number in the dialing screen.
If I check the call history details after the call however, it shows the new number.
How can I get the dialer screen to show the updated number?
public void onReceive(Context context,Intent intent) {
final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String modNumber = "";
if (oldNumber.startsWith("xx")) {
modNumber = oldNumber.replace("xx", "yyy");
this.setResultData(modNumber );
final String newNumber = this.getResultData();
String msg = "Call Intercepted. Old number " + oldNumber + ", new number " + newNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
The android.permission.PROCESS_OUTGOING_CALLS is present in the manifest file.
App is deployed on a phone running Android 5.0.2 (Lollipop)
It seems that if you call
String text = "String<br>String";
Log.d(TAG, text);
it automatically parses the String to take two lines. The same goes for new line (\n) characters. That makes debugging more complicated. Is there a way to tell the logger to give me the exact String?
The arguments to the methods in Log class are Java strings, so escaping special characters is just like in Java. For example,
String text = "String\nString";
Log.d("TEST!!", text);
Will give you:
D/TEST!!﹕ String
String
while:
String text = "String\\nString";
Log.d("TEST!!", text);
will give you:
D/TEST!!﹕ String\nString
in the logcat.
As far as <BR>, I'm not seeing the same effect as you. Specifically,
String text = "String<br>String";
Log.d("TEST!!", text);
Produces:
D/TEST!!﹕ String<br>String
So I am unable to reproduce your actual problem. However, in general special characters in the Log strings are escaped just like any other Java strings. The logger is dumb and there's no settings to automatically escape special characters; you'll have to do this yourself for arbitrary strings. The Log methods just turn around and call println_native.
I use System.getProperty("line.separator")
ArrayList<String> txts = new ArrayList<String>();
txts.add("aoeuaeou");
txts.add("snhsnthsnth");
String msg = TextUtils.join(System.getProperty("line.separator"),txts);
Log.d(TAG, "Bla bla bla: "+ msg );
show in the log like
Bla bla bla: aoeuaeou
snhsnthsnth
At the end of the message, a trailing space seems to be needed.
Log.i("tag", "My message with a blank line following.\n ");
or
Log.i("tag", "Variable 1: " + v1 + " Variable 2: " + v2 + "\n ");
In my application I have to use a some HTML stuff in a string. But HTML is not working as intended. I have to use that string (Text) to send as an email. The sequence I required of HTML is:
Title (in the center)
Image (in the center)
Description (left align)
and then this HTML string is passed to an email intent. But neither image is showing up in the email nor the title text is getting center align. This is how I am doing this all:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "");
it.setType("text/html");
String title = title;
String emailText = emailText;
it.putExtra(Intent.EXTRA_SUBJECT, title);
it.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailText));
this.startActivity(it);
and this is how the emailText is being formed:
emailText = "<p style= 'color:#000000; font:Georgia; font-size:18pt; text-align:center' align = 'center'><b>" + title +" </b></p>"
+"<br/><br />"
+"<img style=\"border:3px solid #173E8C\" src=\'" +imageUrl+"\' width=\"120\" height=\"90\"align=\"center\"/>"
+"<br/><br/>"
+"<p>" + description;
But I am unable to get the required result that I have mentioned right at the top, Any help is appreciated related to the issue. Thanks in advance..:-)
You must specify the type of email through the function setType () :
it.setType("text/html"); // for HTML
it.setType("text/plain"); // for plain text
You can't send a image as email body in android through Intent.
I'm sending an SMS from my app using an Intent. The SMS pulls a string with "\n" and "\t" formatting out of a text view to send out to a contact of the user's choosing.
My problem is that when sending from a device to another device, the SMS only sends the first 15 characters of the message! When the message appears on the first device, it has all the characters in the string. Then only the first 15 of those characters are received by the second device.
// SMS Activity
public void sendSMS(View v) {
mess = ""+Summary.getText().toString();
Uri uri = Uri.parse("smsto:");
Intent txt = new Intent(Intent.ACTION_SENDTO, uri);
txt.putExtra("sms_body", mess);
startActivity(txt);
}
This is how I generated the string for the message. The text view is also being used for showing the string in the App.
for(...){
if{...}
else{
String beer = name[counter];
int amount = amt[counter];
inText += "x"+ amount+ "\t" + beer + "\n";
}
}
Summary.setText(""+inText);
mess = ""+Summary.getText().toString();
Is there a different method to go about sending this string through sms? I know I can do it using SmsManager, but I want to be able to use the phones messaging system (easier because they can choose the contacts). Any examples I can follow?
Could I use a cursor to get the information for the string?
try adding txt.setType("text/plain");
this might solve your problem.
I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.
I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.