I am trying to send text using an intent. I thought it was straight-forward:
public Intent getIntent() {
final Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, getText());
return intent;
}
private String getText(){
// the emoji can be one of many. I removed all that for brevity
final int unicode = 0x1F3C5;
final String emoji = String.valueOf(Character.toChars(unicode));
// final String emoji = "\n1F3C5"; //didn't work either
// I also tried using HTML here but I think I have some wires crossed, so I am not ruling that out yet
return textPartOne + " " + emoji + " " + textPartTwo;
}
As you can see, the EXTRA_TEXT has a special character in it (an emoji). When the text sends from device A the emoji appears like I expect in the message. But on device B (the receiver) the text shows some madness (usually in two different messages):
#.£¡ù¿ ¡ ¡ | | < | | | \ < | [ ¡ { ¡ [ ¡
#H£(ù#æ#a#¤¥¡
¡9#ü#Hù#Θ#=#ø¥p
¡6#Ö#Δì¿ ¡ |
It doesn't matter if I send the message from device A to device B or B to A. I get the same results either way, so I believe it isn't an issue with the emoji not being supported.
Now, if I remove all but the emoji code:
Xäx&
If I remove the emoji code altogether it works like a charm.
But the client has gotta have those emojis...
😤
Some other things that may be of note:
I am using this to manage what intents I am presenting to the user. They could pick MMS, email, Twitter, Facebook or whatever really. I need to support all of these
I am getting the special characters from Emojipedia
I am not displaying the emojis anywhere in the app. They will be shown in Text message, Email, or Facebook/Twitter feed.
There is already an iOS variant of this app who is successfully doing this.
I have tried nearly a dozen different ways with no avail.
Is there anyone that may have some insight on how to properly send special characters?
Edit:
As you can see, I am sending the emoji I desire. But the result is far from what I expect. This is a screenshot of an emulator sending a text to itself.
Here are some of the ways I have tried
String attempt1 = "--";
try {
attempt1 = Html.fromHtml(new String("🏅".getBytes("UTF-8"))).toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
final int codePoint = 0x26F7;
final String attempt2 = new String(new int[]{codePoint}, 0, 1);
final String attempt3 = new String(Character.toChars(codePoint));
final String attempt4 = "\u26F7";
All result in:
&n
If I add text to these:
final String attempt4 = "COME ON: \u26F7";
I get this:
#Σ£(ù#æ#s##£ Å¡7#X£¿ù¿
Which makes total sense he said sarcastically
You can use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int, etc.
In your case, in activity2, before going to activity1, you will store a String message this way :
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", getText());
startActivity(intent);
In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Then you can set the text in the TextView:
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
Hope this helps !
Related
I have a system that has been produced for 2 years now. It is an EMM system for controlling corporate devices.
It uses FireBase to send the functionality executed on the device from the server app to the device.
There are around 400 possible commands you can send to a device and all these commands are handled in one class initially, which overrides the onMessageReceived() from the FireBaseMessagingService class.
The older version of Android studio built the apk which is now in production. I have started to work on version 2 of my system after about a year off. so I updated my Android studio to the latest (4).
The Problem:
when I try to build the project and push onto a device, I get
error: code too large public void onMessageReceived(RemoteMessage remoteMessage) {
As stated before this onMessageReceived method can handle 400 different types of push notifications from the server app, so there are a lot of if/else statements in the method body.
Is there any reason why since the AS upgrade this will not work?
is there any setting I can change in AS to get past this?
What I have tried:
I thought about putting half of the if/else in another service class, to cut down on the method code. This would involve passing the remoteMessageMap to another class to carry on with the if/else processing.
remoteMessageMap from FireBase is a Map and Maps are not serializable as they extend the interface, so can't pass it.
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
AppObj appObj;
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "remoteMessage.getData() = " + remoteMessage.getData());
Map remoteMessageMap = remoteMessage.getData();
String message = (String)remoteMessageMap.get("message");
thanks
[edit1]
else if(message.trim().equalsIgnoreCase("CLEARCACHE_REMOVE_APP_WL")){
Log.e(TAG, "received CLEARCACHE_REMOVE_APP_WL");
String pushGuid = (String)remoteMessageMap.get("pushguid");
Log.e(TAG, "pushGuid = " + pushGuid);
String clearCacheRemoveWhitelist = (String)remoteMessageMap.get("clear_cache_app_names");
Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
intentExecutePushCommand.putExtra("command", message);
intentExecutePushCommand.putExtra("pushguid", pushGuid);
intentExecutePushCommand.putExtra("clear_cache_app_names", clearCacheRemoveWhitelist);
startService(intentExecutePushCommand);
}else if(message.trim().equalsIgnoreCase("CLEARCACHE_GET_PACKAGENAMES_WL")){
Log.e(TAG, "received CLEARCACHE_GET_PACKAGENAMES_WL");
String pushGuid = (String)remoteMessageMap.get("pushguid");
Log.e(TAG, "pushGuid = " + pushGuid);
Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
intentExecutePushCommand.putExtra("command", message);
intentExecutePushCommand.putExtra("pushguid", pushGuid);
startService(intentExecutePushCommand);
}else if(message.trim().equalsIgnoreCase("CLEARCACHE_ADD_PACKAGENAME_WL")){
Log.e(TAG, "received CLEARCACHE_ADD_PACKAGENAME_WL");
String pushGuid = (String)remoteMessageMap.get("pushguid");
Log.e(TAG, "pushGuid = " + pushGuid);
String packageName = (String)remoteMessageMap.get("package_name");
Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
intentExecutePushCommand.putExtra("command", message);
intentExecutePushCommand.putExtra("pushguid", pushGuid);
intentExecutePushCommand.putExtra("package_name", packageName);
startService(intentExecutePushCommand);
}
There is no need to pass the remoteMessageMap to another class. The source of the problem is the limitation in the java method size. Here is a piece of the official documentation of oracle which is related to this problem:
code_length
The value of the code_length item gives the number of bytes in the code array for this method.
The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536.
The point is that your onMessageReceived method is too long, which is bigger than 64KB of compiled code. It is weird why it was compiled fine in previous versions of Android Studio :)
Anyway, the solution is to break the method into smaller fragments. My suggestion is fragmentation by some message types. For example:
private static final String COMMAND_1 = "COMMAND_1";
private static final String COMMAND_2 = "COMMAND_2";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "remoteMessage.getData() = " + remoteMessage.getData());
Map remoteMessageMap = remoteMessage.getData();
String message = (String) remoteMessageMap.get("message");
String type = extrated_from_received_message;
switch (type) {
case COMMAND_1:
handleCommand1(remoteMessageMap);
break;
case COMMAND_2:
handleCommand2(remoteMessageMap);
break;
// more commands ...
default:
// ...
}
}
private void handleCommand1(Map remoteMessageMap){
// do whatever related to command 1
}
private void handleCommand2(Map remoteMessageMap){
// do whatever related to command 2
}
In this way, the method size would be optimized and the performance of calling it will be far improved.
It seems that you are repeating the same lines of code a lot of times, just put these lines of code and maybe a few more in a separate method that is called on each else if and this will reduce the size of onMessageReceived()
Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
intentExecutePushCommand.putExtra("command", message);
intentExecutePushCommand.putExtra("pushguid", pushGuid);
hello i have an app of POS ( point of sale )
i wanted to make it send printing data to be printed through bluetooth printer
i found this solution
https://play.google.com/store/apps/details?id=com.fidelier.posprinterdriver
its a driver for my printer
they say
+### Print from your Android App (interactive user action)
+
+Create your ESC data using the helpers Create an Android Intent using Add your ESC data as a “Data” extra Start the intent.
+You can be printing in minutes with just a couple lines of code. It's as simple as creating your intent, adding your ESC formatted string and start the (service) intent.
+
+Example:
+
+```java
+
+String dataToPrint="$big$This is a printer test$intro$posprinterdriver.com$intro$$intro$$cut$$intro$";
+
+Intent intentPrint = new Intent();
+
+intentPrint.setAction(Intent.ACTION_SEND);
+intentPrint.putExtra(Intent.EXTRA_TEXT, dataToPrint);
+intentPrint.setType("text/plain");
+
+this.startActivity(intentPrint);
+
+```
can any body tell me how can i do that ???
where should i put these codes ? ?
Lets say you have your activity with a button link to this method :
public void click(View v){
Intent intentPrint = new Intent();
intentPrint.setAction(Intent.ACTION_SEND);
intentPrint.putExtra(Intent.EXTRA_TEXT, "Just some dummy text to print");
intentPrint.setType("text/plain");
this.startActivity(intentPrint);
}
By clicking on this button, the intent will be launched, this will probably open a list of application that can handle this kind of intent (ACTION_SEND), choose the correct one and this should do it.
I am a beginner and I just want to convert the data of this logcat mainactivity into string.
I am using pushbots for notifications and I want the "bigText" to be displayed on the screen.
So how am I supposed to do that?
I want to convert the data in the logcat into string .. as you can see the data is in capital letters . please answer as soon as possible .
I am very curious to know. and please someone refer me a good website which can help me for this , it will also be helpful . Thank You
07-29 20:16:03.513 9243-9243/com.example.zia_ali.demopushnotification I/MainActivity﹕ Received message >> Bundle[{BigTextStyle=true, bigText=THIS IS THE TEXT I WANT TO CONVERT INTO STRING, from=381853524749, message=testing BigTextStyle, collapse_key=do_not_collapse}]
v1.
Bundle extras = intent.getExtras();
String message1 = extras.getString("bigText");
TextView t2 = (TextView)findViewById(R.id.textView);
t2.setText(message1);
v2.
Bundle extras = intent.getExtras();
String bigText = null;
if (extras != null && extras.containsKey("bigText")) {
bigText = extras.getString("bigText");
}
TextView t2 = (TextView) findViewById(R.id.textView);
t2.setText(bigText);
So I am trying to add the functionality that when you click on a phone number it would take you to the Dialer app with the pre-populated number. I have the code below:
mContactDetailsText.setText(phonetextBuilder.toString());
Pattern pattern = Pattern.compile("[0-9]+\\s+[0-9]+");
Linkify.addLinks(mContactDetailsText, pattern, "tel:");
and the Text is currently "T. 0123 4567890"
The current outcome is just having the above string without it being clickable. I have even tried added the following line, but to no luck:
mContactDetailsText.setAutoLinkMask(0);
Anyone got any ideas or can see what I am doing wrong?
Thanks
The autolink mask needs to include a search for phone numbers:
mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);
Then you'll need to set the links to be clickable:
mContactDetailsText.setLinksClickable(true);
You might also need movement method set like so:
mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())
You should be able to accomplish what you want with the other answers,
but this will definitely work and will give you more control over the display of the text and what will happen when you click the number.
String text = "T. ";
StringBuilder stringBuilder = new StringBuilder(text);
int phoneSpanStart = stringBuilder.length();
String phoneNumber = "0123 4567890"
stringBuilder.append(phoneNumber);
int phoneSpanEnd = stringBuilder.length();
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
startActivity(intent);
}
public void updateDrawState(TextPaint ds) {// override updateDrawState
ds.setUnderlineText(false); // set to false to remove underline
ds.setColor(Color.BLUE);
}
};
SpannableString spannableString = new SpannableString(stringBuilder);
spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannableString);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
You need to set an onClickListener() on your TextViews. Then they will respond to clicks.
I've created an address book app, and am trying to add some features to it. What I'm trying to do right now is add the ability to longclick a phone number, and then either call/text/mms the number. This all works fine with phones, but I was wondering how to do this on tablets, since they will not have that same ability. The device I'm debugging on is a tablet, and I use HeyWire for texting. I know there are apps out there for calling via WiFi as well. Here's what I have so far for the texting section of my switch statement:
case 1: //SMS
if(CanCallAndText)
{
CustomSMSDialog SendSMSDialog = new CustomSMSDialog(BrowseListActivity.this, ParsedPhoneNum);
SendSMSDialog.setTitle("Sending text to " + PhoneNum);
SendSMSDialog.setCancelable(false);
SendSMSDialog.show();
}
else
{
try
{
Intent WiFiSMS = new Intent(Intent.ACTION_VIEW);
WiFiSMS.setData(Uri.parse("sms:" + PhoneNum));
WiFiSMS.setType("vnd.android-dir/mms-sms");
startActivityForResult(Intent.createChooser(WiFiSMS, ""), 0);
}//endtry
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}//endcatch
}//endelse
break;
I don't know if I'm doing the create chooser incorrectly or not, but it simply tells me that no apps can handle it. Thank you!
EDIT: Ooh, spotted a slight error that I should fix tomorrow. The URI should include ParsedPhoneNum, instead of PhoneNum. Anything other than a number, like a -, would be included in PhoneNum.