populating SMS message in android - java

Here is the code snippet from our app now.
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" ));
sms.putExtra("sms_body", text);
startActivity(sms);
}
This works with the stock SMS app, however a tester reported that with handcent SMS they get a blank message. This prompted me to try it with Google Voice, also get a blank message there.
Is there some way I can get my message text to work with all of these other SMS apps?
We have a clipboard functionality, so a poor workaround at least would be having user push clipboard button, then use their messaging app of choice.

Android tries to reuse Intents as much as possible. In some cases when you start an intent, android doesn't create a new Intent and reuse previous intents. To avoid this set this flag of Intent:
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This flag forces Android to create a new Task with the intent's Activity on top of stack.

When you pass this intent , this will be delivered to all those apps which have registered an activity to receive it. In otherwords you have to use the intent that is registered by the application that u intent to handle your intent

Related

How to start an Android App by receiving a SMS?

I have written an Application, which checks when you receive a new SMS and if its from a certain number, the app plays the ringtone. Now, the App should check if there is a new SMS even if it isn´t lauchned, and so my question is: How can I launch my App by receiving a new SMS?
I tryed to upload my existing code but it didn´t worked, I am sorry.
To start an activity from almost everywhere you can simply call:
Intent myIntent = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(myIntent);
That should also work from your BroadcastReceiver which is triggered while receiving the sms.

Android: Intent Extras not passed with startActivity() outside Activity context

I have a class that listens to SMS broadcast and, it then open an activity in the same application to display the message. This is part of a larger project.
Since the broadcast class does not extend the activity class, calling an Activity with intent requires that I flag it with Intent.FLAG_ACTIVITY_NEW_TASK.
Now the Intent has an extra (the SMS message and some parameters as a single string) which i want to send with the intent. All went well but the Extras are not sent with the intent.
Here are my code
From the broadcast class
//After getting the message into String S and doing some process
Intent inte = new Intent(context.getApplicationContext(), StartApp.class);
inte.putExtra("messageAsString", S.toString()); //Where S is declared somewhere
inte.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(inte);
From the activity recieving the Intent in the onCreate() method
if(getIntent().hasExtra("messageAsString")) {
String message = getIntent().getStringExtra("messageAsString").toString();
Toast toast = Toast.makeText(this, "Received SMS: " + message , Toast.LENGTH_LONG);
toast.show();
}
The application may have already running, the home button has been pressed. Either way, what I want to achieve is that as the registered reciever receives the message, I want to start an activity automatically and pass the message and its meta data to it.
I know I'm missing something somewhere but will appreciate your swift help
You also need to handle onNewIntent(), which may be called if the activity already exists and startActivity() brings an existing instance back to the foreground. Your extra is probably in there.

Query regarding Android push notifications

I have an Android app with a working push notification set up. It works currently like this:
User gets a push notification
When the user taps on the push notification, he is taken to a screen which has the list of all the notifications. It is basically a list view which has all the notifications.
Now, want to implement the following:
a. When a new notification comes to the user, it has to be displayed on the list view, irrespective of whether a user taps on it.
b. I have to indicate the number of notifications that user has received on the app icon. For instance, if you get a message in whats app, it displays the number of messages on the app icon.
Could anybody let me know what is the best possible way to implement these functionality? If anybody could point me to tutorials/references, it would be very helpful.
Thanks!
a. Launch an activity from the receiver(GCM/C2DM receiver) when the push notification arrives
// Your C2DM receiver (for GCM check the Android Documentation)
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(com.google.android.c2dm.intent.RECEIVE))
{
//start your list view activity of notifications
Intent i = new Intent();
i.setClassName("com.test", "com.test.NotificationsActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
b. Check this.

Android4.4 can not handle sms intent with "vnd.android-dir/mms-sms"

My application has a button to start default sms activity and
it worked perfectly fine all android version except new one, Android 4.4(kitkat)
Here is the code:
public void onClick(View arg0) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", member.getPhoneNumber().trim());
context.startActivity(smsIntent);
}
And I get error messages
11-08 02:08:32.815: E/AndroidRuntime(14733): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
I know that google made some changes on how the default sms app handles sms intents.
but my app is not a sms app but it only has function to start default sms app with recipient number. so please help.
To start the SMS app with number populated use action ACTION_SENDTO:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
startActivity(intent);
This will work on Android 4.4. It should also work on earlier versions of Android however as the APIs were never public the behavior might vary. If you didn't have issues with your prior method I would probably just stick to that pre-4.4 and use ACTION_SENDTO for 4.4+.
to start the sms app without insert the number, you should delete setType
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"));
intent.putExtra("sms_body", "smsMsgVar");
startActivity(intent);
The issue happened when you are testing with Emulator,
Please test with the actual device.
In Kotlin this code works:
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
if (defaultSmsPackageName != null){ //Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
sendIntent.setPackage(defaultSmsPackageName)
activity!!.startActivity(sendIntent)
}

Android - Getting values from a notification

I can't find anything on this on google, nor here? But I am trying to create a notification and retrieve the value of an EditText field that has been set as a custom view for the notification
Is there anyway to retrieve this value? As notification's (as far as I'm aware) can just be set and/or updated.. but there must be some way!
That's not possible. The actual EditText view inside your notification is not even owned by your process - it's instantiated by the system using the RemoteViews object you are providing. The only way of communicating with it is by sending back predefined intents, for example via setOnClickPendingIntent. You cannot retrieve a content of EditText that way, though.
As a solution I'd recommend to display a dialog or activity with your EditText when user taps the notification, and handle their input there.
In your notification Intent
Intent notificationIntent = new Intent(this, FlightActivity.class);
Bundle bundle = new Bundle();
bundle.putString("whatever",YOUR_STRING_VALUE);
notificationIntent.putExtras(bundle);
When the application starts, you can get it using the intent
Is this what you want?

Categories