Is there any way that I could directly redirect the log message in Android to a string?
For example, I have a log message in Android.
Log.v(TAG, "First Name" + firstName);
Log.v(TAG, "Last Name" + lastName);
I have a string
String nameMessage = "Please check your credetials\n" ;
I want to concatenate the log to the message.
One way is to concatenate manually.
I am looking If we could redirect the log to message.
I'm not sure if I am understanding your question right, but technically you could just create a function that appends the log message and then actually logs it?
Example:
private StringBuilder errorMessage;
public void log(String tag, String message) {
errorMessage.append(message).append(System.getProperty("line.separator"));
Log.v(tag, message);
}
public void getErrorMessage() {
return errorMessage.toString();
}
And then, you'd just replace all your calls to Log#v with a call to #log(tag, message).
Related
I have an app in which server sends some push notification using GCM server, The implementation is done on both side but i am facing problem is that i can't get notification from message body, it is always showing me "null". But i can see message in messag body using debug.
String message = bundle.getString("message");
Log.e(TAG, "onMessageReceived::" + message);
Log.e(TAG, "from::" + from);
and message body is :-
Bundle[{google.sent_time=1497866966996, google.message_id=0:1497866967011288%466cbbd8466cbbd8, notification=Bundle[{priority=high, body=Your wallet has been credited with 1.000 point(s)., title=Wallet credited}], collapse_key=com.s.baty}]
Try ,
Bundle notificationData = bundle.getBundle("notification");
String body = notificationData.getString("body");
It received correct data only
String bodymessage = bundle.getString("body");
Problem is print message after converting Sting
Log.e(TAG, "onMessageReceived::" + message.toString());
You can receive message in onMessage() function of GCMIntentService implementing class. I have created a function for getting proper message and key.
#Override
protected void onMessage(Context context, Intent intent) {
dumpIntent(intent);
}
public void dumpIntent(Intent i) {
Bundle bundle = i.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
Log.e("Intent Data", "[" + key + "=" + bundle.get(key) + "]");
}
}
}
After that you will easily set message with NotificationManager.
I am using firebase crash in my cordova application and I want to capture all logs ( Log.e ) in my application as follows.
Login => {"userName" : "jhon" , "networkConnection": "on" ,
"status":"success"}
AddContact => {"userName" : "jhon" , "networkConnection": "on" ,
"status":"success", "contactId" :"3233"}
But I have noticed one thing is that if my app gets crashed then only I am able to see logs without that I can't see the normal logs when I app do not crash.
For example ,
In following code
If I explicitly call FirebaseCrash.report(new Exception(name)); then only FirebaseCrash.logcat(Log.ERROR, name, params + " "+message); appears in firebase crash report.
If I only write FirebaseCrash.report(new Exception(name)); then only FirebaseCrash.logcat(Log.ERROR, name, params + " "+message); the these logs are not coming to the server.
private void logCrash(final CallbackContext callbackContext, final String name, final String params,final String message){
cordova.getThreadPool().execute(new Runnable() {
#Override
public void run() {
FirebaseCrash.logcat(Log.ERROR, name, params + " "+message);
FirebaseCrash.report(new Exception(name));
}
});
callbackContext.success();
}
I want to make a http server in android which serve file (html/png..) also doing event based on request.
Example: If request is /maketoast android make a toast or request is /chanhetext android change a specific textview text.
Point is: I already make a server by Nanohttpd. It's serve files, but it don't make any event, like make toast or change textview text.
Here is Nanohttpd serve method
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
} else {
msg += "<p>Hello, " + parms.get("username") + "!</p><img src=max.png/>";
}
//Toast.makeText(mContext, "Helloooooo...!!!", Toast.LENGTH_SHORT).show();
return newFixedLengthResponse( msg + "</body></html>\n" );
}
When I try to make toast server going freeze and doesn't give any response. Can any one please tell me how I make this kind of thing.
Try:
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(mContext, "Helloooooo...!!!", Toast.LENGTH_SHORT).show();
}
});
Put your Nanohttpd into a service, so that you can send a LocalBroadCast to your activity to display Toast or any other UI response.
I have probably a simple question but has me stumped. For my Android Wear application, I have two sensors working (step counter and heartrate).The wear app then sends these values back to the mobile application. I am sending them using the Message API. My stepcount sendMessage() and heartrate sendMessage() method look the same. Here is my heartrate sendMessage method.
private void sendMessageToHandheld(final String message) {
if (mGoogleApiClient == null)
return;
Log.d(LOG_TAG,"sending a message to handheld: "+message);
// use the api client to send the heartbeat value to our handheld
final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
#Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
final List<Node> nodes = result.getNodes();
if (nodes != null) {
for (int i=0; i<nodes.size(); i++) {
final Node node = nodes.get(i);
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), message, bytes);
}
}
}
});
}
Problem is I am only using one messageReceived method on the mobile. So I cant differentiate from the step value coming in or the heartrate value coming in.
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
Log.d(LOG_TAG, "received a message from wear: " + messageEvent.getPath());
if (messageEvent.getPath().contains("HEARTBEAT")){
// save the new heartbeat value
currentValue = Integer.parseInt(messageEvent.getPath());
if(handler!=null) {
// if a handler is registered, send the value as new message
Log.d(LOG_TAG, "received a heartbeat message from wear: " + messageEvent.getPath());
handler.sendEmptyMessage(currentValue);
}
}
else {
// save the new steps value
currentStepValue = Integer.parseInt(messageEvent.getPath());
if (handler != null) {
// if a handler is registered, send the value as new message
Log.d(LOG_TAG, "received a step message from wear: " + messageEvent.getPath());
handler.sendEmptyMessage(currentStepValue);
}
}
I tried passing in a byte array into the Heartrate sendMessage() and other strings as flags so that I could tell the values apart but no luck. Anyone know what the best way to go about this would be?
Any help would be appreciated.
Cheers
It seems you are sending the data inside the path attribute. This is not the correct use of this parameter.
Let's take a look at the MessageApi.sendMessage(GoogleApiClient client, String nodeId, String path, byte[] data method.
What you want to do is use String path to provide identifier for your message, for example in your case it would be step_counter and heartbeat. This way you can identify it on the other side, when you receive the message.
The sensor data should go into data field. You can put it raw there, but a better way is to create a DataMap and then serialize it into byte[]. This way you can enrich the data later easily.
My android app getting same message like "you got message". Even though i'm changing data in server side.
Server-Side code(dot net):
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
+ Label1.Text + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + regId + "";
have to make any changes in GCM app code?
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = getString(R.string.gcm_message);
//String message = intent.getExtra("message");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
On seraching google i found something that we have to replace this
String message = getString(R.string.gcm_message);
with
String message = intent.getExtra("message");
but i am getting error like this "The method getExtra(String) is undefined for the type Intent".Please guide me that what i'm missing in this?
use
String message = intent.getStringExtra("message");
instead of
String message = intent.getExtra("message");
for Getting String Message from Intent
EDIT :
if you are receive data in Bundle instance then change your code as because getExtra is not a method in Intent class :
Bundle bundle = intent.getExtras("bundle_Name_here");
now retrieve all value from bundle using key
Add this to your HTTP POST:
data.notId="2"
For each different notId it will be threated as a different message. I had the same problem for a long time, now I would like to group those messages.