launch sms application with an intent

2019-01-01 03:52发布

问题:

I have a question about an intent... I try to launch the sms app...

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType(\"vnd.android-dir/mms-sms\");
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
    Intent.FLAG_ACTIVITY_CLEAR_TOP;
intent.setFlags(flags);
intent.setData(Uri.parse(\"content://sms/inbox\"));
context.startActivity(intent);

so, you can see that I put too much things in my intent, but that\'s because I don\'t know how I can do... Thank\'s

回答1:

To start launch the sms activity all you need is this:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);         
sendIntent.setData(Uri.parse(\"sms:\"));

You can add extras to populate your own message and such like this

sendIntent.putExtra(\"sms_body\", x); 

then just startActivity with the intent.

startActivity(sendIntent);


回答2:

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType(\"vnd.android-dir/mms-sms\");
smsIntent.putExtra(\"address\", \"12125551212\");
smsIntent.putExtra(\"sms_body\",\"Body of Message\");
startActivity(smsIntent);


回答3:

If android version is Kitkat or above, users can change default sms application. This method will get default sms app and start default sms app.

private void sendSMS() {    
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
      {
         String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19

         Intent sendIntent = new Intent(Intent.ACTION_SEND);
         sendIntent.setType(\"text/plain\");
         sendIntent.putExtra(Intent.EXTRA_TEXT, \"text\");

         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);
         }
         startActivity(sendIntent);

      }
      else // For early versions, do what worked for you before.
      {
         Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
         smsIntent.setType(\"vnd.android-dir/mms-sms\");
         smsIntent.putExtra(\"address\",\"phoneNumber\");         
         smsIntent.putExtra(\"sms_body\",\"message\");
         startActivity(smsIntent);
      }
   }


回答4:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType(\"vnd.android-dir/mms-sms\");
startActivity(intent);

That\'s all you need.



回答5:

If you want to launch SMS Composing activity from some of your other activity and you also have to pass a phone number and SMS text, then use this code:

Uri sms_uri = Uri.parse(\"smsto:+92xxxxxxxx\"); 
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri); 
sms_intent.putExtra(\"sms_body\", \"Good Morning ! how r U ?\"); 
startActivity(sms_intent); 

Note: here the sms_body and smsto: is keys for recognizing the text and phone no at SMS compose activity, so be careful here.



回答6:

Here is the code that will open the SMS activity pre-populated with the phone number to which the SMS has to be sent. This works fine on emulator as well as the device.

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType(\"vnd.android-dir/mms-sms\");
smsIntent.setData(Uri.parse(\"sms:\" + phoneNumber); 


回答7:

Use

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(\"com.android.mms\", \"com.android.mms.ui.ConversationList\");


回答8:

Intent eventIntentMessage =getPackageManager()
 .getLaunchIntentForPackage(Telephony.Sms.getDefaultSmsPackage(getApplicationContext));
startActivity(eventIntentMessage);


回答9:

I use:

Intent sendIntent = new Intent(Intent.ACTION_MAIN);
sendIntent.putExtra(\"sms_body\", \"text\");
sendIntent.setType(\"vnd.android-dir/mms-sms\");
startActivity(sendIntent);


回答10:

try {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setData(Uri.parse(\"smsto:\" + Uri.encode(number)));
    smsIntent.putExtra(\"address\", number);
    smsIntent.putExtra(\"sms_body\", message);

    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> resInfo = pm.queryIntentActivities(smsIntent, 0);

    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;

        if (packageName.contains(\"sms\")) {
            //Log.d(\"TAG\", packageName + \" : \" + ri.activityInfo.name);
            smsIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        }
    }
    activity.startActivity(smsIntent);
} catch (Exception e) {
    // Handle Error
}

Best way of doing this.



回答11:

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
//CHANGE YOUR MESSAGING ACTIVITY HERE IF REQUIRED 
sendIntent.setClassName(\"com.android.mms\", \"com.android.mms.ui.ComposeMessageActivity\");
sendIntent.putExtra(\"sms_body\",msgbody); 
sendIntent.putExtra(\"address\",phonenumber);
//FOR MMS
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(\"file:///sdcard/mms.png\"));
sendIntent.setType(\"image/png\");
startActivity(sendIntent);


回答12:

You can open default sms App and pass details as below :
Note : If u want to send to many numbers, separate each number with \";\" inside string

String mblNumVar = \"9876543210;9123456789\";
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse(\"sms:\" +  mblNumVar));
smsMsgAppVar.putExtra(\"sms_body\", \"Hello Msg Tst Txt\");
startActivity(smsMsgAppVar);

|Or| Use this function :

void openSmsMsgAppFnc(String mblNumVar, String smsMsgVar)
{
    Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
    smsMsgAppVar.setData(Uri.parse(\"sms:\" +  mblNumVar));
    smsMsgAppVar.putExtra(\"sms_body\", smsMsgVar);
    startActivity(smsMsgAppVar); 
}


回答13:

The below code works on android 6.0.
It will open the search activity in the default messaging application with the conversations related to specific string provided.

Intent smsIntent = new Intent(Intent.ACTION_MAIN);
        smsIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        smsIntent.setClassName(\"com.android.mms\", \"com.android.mms.ui.SearchActivity\");
        smsIntent.putExtra(\"intent_extra_data_key\", \"string_to_search_for\");
        startActivity(smsIntent);  

You can start the search activity with an intent. This will open the search activity of the default messaging application. Now, to show a list of specific conversations in the search activity, you can provide the search string as string extra with the key as

\"intent_extra_data_key\"

as is shown in the onCreate of this class

String searchStringParameter = getIntent().getStringExtra(SearchManager.QUERY);
    if (searchStringParameter == null) {
        searchStringParameter = getIntent().getStringExtra(\"intent_extra_data_key\" /*SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA*/);
    }
    final String searchString = searchStringParameter != null ? searchStringParameter.trim() : searchStringParameter;

You can also pass the SENDER_ADDRESS of the sms as string extra, which will list out all the conversations with that specific sender address.

Check com.android.mms.ui.SearchActivity for more information

You can also check this answer



回答14:

The best code that works with Default SMS app is.

Uri SMS_URI = Uri.parse(\"smsto:+92324502****\"); //Replace the phone number
Intent sms = new Intent(Intent.ACTION_VIEW,SMS_URI);    
sms.putExtra(\"sms_body\",\"This is test message\"); //Replace the message witha a vairable 
startActivity(sms);


回答15:

on emulator this work for me

Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromParts(\"sms\", number, null));
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra(\"sms_body\", remindingReason);

                startActivity(i);


回答16:

Sms Intent :

Intent intent = new Intent(\"android.intent.action.VIEW\");
        /** creates an sms uri */
        Uri data = Uri.parse(\"sms:\");
        intent.setData(data);


回答17:

Compose SMS :

Uri smsUri = Uri.parse(\"tel:\" + to);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra(\"address\", to);
intent.putExtra(\"sms_body\", message);
intent.setType(\"vnd.android-dir/mms-sms\");
startActivity(intent);


回答18:

private void sendSMS() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
        {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getActivity()); // Need to change the build to API 19
            Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
            sendIntent.setType(\"text/plain\");
            //sendIntent.putExtra(\"address\", add);
            sendIntent.setData(Uri.parse(\"smsto:\" + add));
            sendIntent.putExtra(\"sms_body\", getString(R.string.invitation_message));
            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);
            }
            startActivity(sendIntent);

    } else // For early versions, do what worked for you before.
    {
        Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
        smsIntent.setType(\"vnd.android-dir/mms-sms\");
        smsIntent.putExtra(\"address\", add);
        smsIntent.putExtra(\"sms_body\", \"message\");
        startActivity(smsIntent);
    }
}