I'm trying to start the SMS activity with the phone number from a scanned QR Code filled in. So far I've been able to get the activity to launch but the number and message fields are blank. Here's what I have, currently this is crashing the app:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
Uri uri = Uri.parse(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("vnd.android-dir/mms-sms");
i.setData(uri);
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
The String smsUri contains the scanned string from the QR Code, "SMSTO:666-666-1234:hello". How can I get the SMS Activity to launch with the phone number and the message already entered into the number and body fields?
I saw this post:
Sending SMS using Intent does not add recipients on some devices
Would I need to parse the QR Code result myself and break it into the phone number and message, then add those as Extras like that example?
Got it!
Since I can't answer my own question yet here it is:
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
Related
I searched stack overflow and the google documentation for email intents and found that code to be the go to code:
However, everytime I click "submit" in my activity I choose gmail. It opens a new email in gmail, but it only puts the receipient address. The subject and the text(name, email address and feedback text) are missing.
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
I implemented that as the following code. Everything after the else is to have a AlertDialog pop up if there is no app installed.
public class EmailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitvity_email);
final EditText nameField = (EditText) findViewById(R.id.editTextName);
final EditText addressField = (EditText) findViewById(R.id.editTextEmail);
final EditText subjectField = (EditText) findViewById(R.id.editTextSubject);
final EditText feedbackField = (EditText) findViewById(R.id.editTextFeedback);
final Button submitFeedback = (Button) findViewById(R.id.buttonSubmitFeedback);
final String name = nameField.getText().toString();
final String address = addressField.getText().toString();
final String subject = subjectField.getText().toString();
final String feedback = feedbackField.getText().toString().concat(name).concat(address);
submitFeedback.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
composeFeedback(subject, feedback);
}
});
}
public void composeFeedback(String subject, String feedback){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + "mydeveloperemail#gmail.com"));
intent.putExtra(Intent.EXTRA_TEXT,feedback);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
final Drawable fail = getResources().getDrawable(R.drawable.ic_fail);
AlertDialog.Builder builder = new AlertDialog.Builder(EmailActivity.this);
builder.setTitle("Fehler");
builder.setIcon(fail);
builder.setMessage("Keine Email App verfügbar!");
builder.setCancelable(false);
builder.setNeutralButton("Okay", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Edit: deleted intent filter as #CommonsWare pointed out in his answer
ACTION_SENDTO is not required to use any extras. Put your data in the mailto: Uri, the way a Web page would.
In the android manifest i declared an intent filter to the activtiy
Unless you are writing an email app, like Gmail, this <intent-filter> is both unnecessary and damaging to users.
I try the following code to send messages to line app. It works; however, before I send message,it will move to the line friends page and I have to choose friends whom I want to send the messages to. How could I modify the code that I could choose friends at code instead of choosing friends manually.
public class MainActivity extends AppCompatActivity {
static final int REQUEST_ACTION_PICK = 1;
public static final String PACKAGE_NAME = "jp.naver.line.android";
public static final String CLASS_NAME = "jp.naver.line.android.activity.selectchat.SelectChatActivity";
private List<ApplicationInfo> m_appList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendTextHandler(this);
}
public void sendTextHandler(MainActivity view) {
String sendText = ((TextView)findViewById(R.id.send_text)).getText().toString();
if(checkLineInstalled()){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName(PACKAGE_NAME, CLASS_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, sendText);
startActivity(intent);
}else{
Toast toast = Toast.makeText(this, "LINEがインストールされていません", Toast.LENGTH_SHORT);
toast.show();
}
}
private boolean checkLineInstalled(){
PackageManager pm = getPackageManager();
m_appList = pm.getInstalledApplications(0);
boolean lineInstallFlag = false;
for (ApplicationInfo ai : m_appList) {
if(ai.packageName.equals(PACKAGE_NAME)){
lineInstallFlag = true;
break;
}
}
return lineInstallFlag;
}
}
The code is from https://gist.github.com/ekos/3993270.
If you want to be able to open a specific user based on the ID you can do the following:
String userId = findUserId();
String sendText = "line://ti/p/~" + userId;
Intent intent = null;
try {
intent = Intent.parseUri(sendText, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(intent);
If you want to make a message and then be able to choose who to send it to you can use the following:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("line://msg/text/" + getMessage()));
startActivity(intent);
I am attempting to get my app to offer me a choice of what browser to use. I have assigned the URL to http://google.com. When I run it, I get a 'No apps can perform this actions' error. I'm missing something and i cant figure it out.
public class myClass{
static private final String URL = "http://www.google.com";
..
..
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
startImplicitActivation();
}
});
private void startImplicitActivation() {
Log.i(TAG, "Entered startImplicitActivation()");
Intent baseIntent = new Intent(Intent.ACTION_SEND,Uri.parse(URL));
String title = "Choose Browser";
Intent chooserIntent = Intent.createChooser(baseIntent, title);
Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
startActivity(chooserIntent);
}
...
...
}
To view a Web page, use ACTION_VIEW, not ACTION_SEND.
I have activity A where it has a ADD button when I click on the button it displays the list of users with check boxes in Second Activity and when I select the users and click the conform button it should display in all the users Activity A.I am using adapter to display the users.
from this button I am getting the list of checked values
private void checkButtonClick()
{
conform = (Button)findViewById(R.id.Confirm);
conform.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Namelist> stateList = contactadapteruser.listexample;
Bundle b=null;
Namelist state;
for(int i=0;i<stateList.size();i++)
{
state = stateList.get(i);
b = new Bundle();
b.putString("selected",state.getName());
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Intent i= new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
}
});
}
from this button I am getting the I am retrieving the values
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = getIntent();
Bundle b1 = i2.getExtras();
String name1 = b1.getString("selected");
tweet.setText(name1+",\t");
}
});
My problem I am unable to get the users which are checked.
Can any one help me .
use start activity for result to start your second activity.
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
from second activity before closing you can send
Intent return_intent = new Intent();
return_intent.putExtra("your_user_list",user_list);
setResult(RESULT_CODE,return_intent);
finish();
Now in your FirstActivity override onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_CODE){
// GET YOUR USER LIST HERE AND USE IT FOR YOUR PURPOSE.
user_list=data.getExtra("your_user_list");
}
}
}
You can use broadcast sender and receiver also for your case. But above method is good for your task.
ArrayList<Namelist> stateList = contactadapteruser.listexample;
// convert your ArrayList to a String[]
String[] arr = new String[stateList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = stateList.get(i).getName();
}
Then using this question as a source, put the String array in your intent:
Bundle b = new Bundle();
b.putStringArray("selected", arr);
Intent i = new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
Then to read the String array from your other activity:
Bundle b = getIntent().getExtras();
String[] array = b.getStringArray("selected");
public class AndroidEmailActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittextEmailAddress = (EditText) findViewById(R.id.email_address);
final EditText edittextEmailSubject = (EditText) findViewById(R.id.email_subject);
final EditText edittextEmailText = (EditText) findViewById(R.id.email_text);
Button buttonSendEmail_intent = (Button) findViewById(R.id.sendemail_intent);
buttonSendEmail_intent.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String emailAddress = edittextEmailAddress.getText().toString();
String emailSubject = edittextEmailSubject.getText().toString();
String emailText = edittextEmailText.getText().toString();
String emailAddressList[] = { emailAddress };
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
intent.putExtra(Intent.EXTRA_TEXT, emailText);
startActivity(Intent.createChooser(intent,
"Choice App t send email:"));
}
});
}
}
This is my code.I am not getting any emails.I need to send emails through this intent method.how can I acheive that?Where I went wrong?pls give some suggestions.
I am getting the error when I click the Send email button as "No applications can perform this action."
This is my code to send emails:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.share_email_subject));
sendIntent.setType("message/rfc822");
activity.startActivity(Intent.createChooser(sendIntent, "Choose Email Client"));
Try removing the plain/text type?
It is Good practice to work with original device rather than on Virtual Device. I suggest you try this code block on real device.
Intent sendEmail= new Intent(Intent.ACTION_SEND);
sendEmail.setType("plain/text");
startActivity(Intent.createChooser(sendEmail, "Email:"));
If you want to see more on Email with Intent then refer to this post:
http://androidtutforbeginner.blogspot.com/2012/03/send-email-with-intent-in-android.html