Sunday, January 10, 2016

(Urgent) android receiving Chinese garbled messages


This post last edited by the yuzhisheng on 2013-04-10 11:05:00
Specific implementation code below, you can receive text messages.
I put this program package installed on your phone, then use another cell phone to send a message on this phone, the phone can normally receive Chinese SMS, but
The content of the message written under sdcard / sysdata open look, Chinese characters are all garbled.
This is what causes it, is coded and the received message character encoding is not inconsistent when I write the file?
Just getting started, trouble look at my code where the problem, willing to please help out, thank you!


 public class SMSBroadcastReceiver extends BroadcastReceiver {
private String TAG = "SMSBroadcastReceiver"; // This sets a Log flag, easy to debug

/ *
* This method
executed when receiving the information* /
Override
public void onReceive (Context context, Intent intent) {
// Receive information from the Intent
Log.i (TAG, "receive SMS");
Object [] pdus = (Object []) intent.getExtras () get ("pdus");.

for (Object p: pdus) {
byte [] sms = (byte []) p;
SmsMessage message = SmsMessage.createFromPdu (sms);
// Get the message content
String content = message.getMessageBody ();
// Get the transmission time
Date date = new Date (message.getTimestampMillis ());
// Get the sender's number
String number = message.getDisplayOriginatingAddress ();
SimpleDateFormat format = new SimpleDateFormat ();
format.applyLocalizedPattern ("yyyy-MM-dd HH: mm: ss");
// Process the data ...
StringBuilder msg = new StringBuilder ();
msg.append ("time:" + format.format (date));
msg.append ("\ n");
msg.append ("from:" + number);
msg.append ("\ n");
msg.append (content);
// SmsManager.getDefault (). SendTextMessage ("5554", null,
// Msg.toString (), null, null);
format.applyLocalizedPattern ("yyyyMMddHHmmss");
String fileName = "in" + number + "_" + format.format (new Date ());

Log.i (TAG, msg.toString ());

try {
String sdcardpath = Environment.getExternalStorageDirectory ()
.getAbsolutePath ();
/ * Create sysdata folder * /
File f = new File (sdcardpath + "/ sysdata /");
if (! f.exists ()) {
f.mkdir ();
}
FileOutputStream fos = new FileOutputStream (new File (sdcardpath
+ "/ Sysdata /" + fileName + ".txt"));
BufferedWriter br = new BufferedWriter (new OutputStreamWriter (
fos));
br.write (msg.toString ());
br.close ();
Log.i (TAG, "the message has been saved");
} Catch (IOException e) {
Log.i (TAG, e.getMessage ());
}
}
}
}

Reply:
Can store description is not a code problem, the problem is coded, try a different encoding.
Reply:
quote 1 floor wlcw16 reply:
can store description is not a code problem, a coding problem, try a different encoding.

Thank you for your reply, tried to sdcard in writing file GBK, GB2312, UTF-8, use RootExplorer in the simulator is still open is garbled "????".
I guess it is not a cell phone to send text messages using these codes is simply not it?
Reply:
Read SMS when used in what way?
Try to use String to extract messages, see if there is garbled.
Reply:
Good is not read
Reply:
quote 3rd floor wlcw16 reply:
read text messages when what manner it?
Try to use String to extract messages, see if there is garbled.

Read SMS text messages using a String Gets the time, numbers, text, and then use the acquired StringBuilder string makes up a complete message, the last used StingBuilder FileOutputStream to write the file's contents.

Distortion is written to the file produced by this step may be encoding and acquired by the content of the message is not the same when writing to a file, but I do not know what the content of messages using a code.
Reply:
What you are running buddy app
Reply:
quote, 6th Floor congjianfei reply:
What you guys running app


Run in the simulator, and in his own cell phone running, the lingering distress of Chinese garbled.
Reply:
Man in String content = sms.getMessageBody (); direct Toast look to try to see garbled incorrect?
Reply:
quote 8th floor congjianfei reply:
Man in String content = sms.getMessageBody (); direct Toast look to try to see garbled incorrect?


Toast pop-up content is garbled, it is "????."
Reply:
import android.telephony.CbSmsMessage;

private Uri storeCBMessage (Context context, CbSmsMessage [] msgs) {
CbSmsMessage sms = msgs [0];

// Store the message in the content provider.
int pduCount = msgs.length;
StringBuilder body = new StringBuilder ();
if (pduCount == 1) {
// There is only one part, so grab the body directly.
body.append (sms.getMessageBody ());
} Else {
// Build up the body from the parts.
for (int i = 0; i & lt; pduCount; i ++) {
sms = msgs [i];
body.append (sms.getMessageBody ());
}
}

ContentResolver resolver = context.getContentResolver ();
ContentValues values = getCBContentValue (sms, body.toString ());
return resolver.insert (MESSAGE_URI, values);
}


This is inside the body.toString () is what you want to write to the database
Reply:
quote the 10th floor a328014942 reply:
import android.telephony.CbSmsMessage;

private Uri storeCBMessage (Context context, CbSmsMessage [] msgs) {
CbSmsMessage sms = msgs [0];

// Store the message in the content provide ......





CbSmsMessage [] msgs = Intents.getMessagesFromIntent (intent);
Reply:
quote the 10th floor a328014942 reply:
import android.telephony.CbSmsMessage;

private Uri storeCBMessage (Context context, CbSmsMessage [] msgs) {
CbSmsMessage sms = msgs [0];

// Store the message in the co ......


Hello, I understand what you mean.
I want to do is actually backup SMS, when the phone receives an SMS message content is written by a program in sdcard.
The only trouble is backed Chinese SMS, open the file in sdcard is garbled, can provide ideas, it is a step in which mistakes lead to a distortion of production.
Reply:
If you String content = sms.getMessageBody (); This step is garbled, then it should be received encoded message not
Turn one other coding try
content = new String (sms.getMessageBody (), "GBK");
Or
content = new String (sms.getMessageBody (), "UTF-8");
and many more.
Reply:
quote 13th floor yinke22222 reply:
If you String content = sms.getMessageBody (); This step is garbled, then it should not be received encoded message
Turn one other coding try
content = new String (sms.getMessageBody (), "GBK");
Or
content = new String (sms.getMessageBody (), "UTF-8");
And so on ......


Thank you for your patience reply, still garbled, headache
Reply:
If you try to change other phones
Reply:
Help knees, backed Chinese short message garbled,
Reply:
In fact, to achieve the function is SMS backup SMS messages, will receive written in .txt format sdcard, but when the message containing Chinese, .txt backup after opening is garbled, stuck, help you, to give the idea
Reply:

Reply:
Refer to the android source is how do chanting, you are saving or save the contents pdu pdu parsed it? If the latter, you should be able to parse the content of arbitrary control ah.
Reply:
quote 19th floor baimy1985 reply:
refer to the android source is how do the chant, you are saving or save the contents pdu pdu parsed it? If the latter, you should be able to parse the content of arbitrary control ah.


SMS data is taken from pdu in
Object [] pdus = (Object []) intent.getExtras () get ("pdus");.
byte [] sms = (byte []) p;
SmsMessage message = SmsMessage.createFromPdu (sms);
// Get the message content
String content = message.getMessageBody ();

I think SmsMessage message = SmsMessage.createFromPdu (sms); where this step is Chinese garbled, but do not know specifically what the phone to send Chinese message default encoding is. Many third-party applications can receive and send SMS text messages in Chinese, it is the encoding and decoding should lead to distortion.


Reply:
Coding problem, and then look at the code
Reply:
Finally get the Chinese SMS garbled
Reply:
Landlord get to everyone to share ah
Reply:
quote the 22nd floor yuzhisheng reply:
Finally get the Chinese SMS garbled
Will the amount under the city how to solve.
Reply:
Brother, I now also do text messaging extraction, appear in Chinese garbled, you in the end is how to solve, give me about ah
QQ: 3093159067
Reply:
FML, the landlord get flash people ...........

No comments:

Post a Comment