想要在createChooser方法中通过sms选项添加共享

时间:2017-07-11 05:07:56

标签: java android android-intent

通过使用shareIntent我只能选择“电子邮件,驱动器,蓝牙和共享”。但我也想在我的shareIntent的createChooser方法中添加sms选项。我可以使用sendIntent对象来做到这一点。任何人都可以帮我这样做吗?提前谢谢!

package com.example.vikrant.shareapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button sharebtn,smsbtn;
    String msg = "Welcome to Share app";   // this is the static mssg 
    which our app shows
    Intent shareIntent,sendIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharebtn = (Button) findViewById(R.id.button);
        smsbtn = (Button) findViewById(R.id.smsbtn);
        external_permissions();
    }

    public void external_permissions(){

        if(ContextCompat.checkSelfPermission(this, 
      Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{ 
    Manifest.permission.SEND_SMS}, 1);
        }
        else{
            sharebtn.setOnClickListener(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
            sharebtn.setOnClickListener(this);
        }
    }
    @Override
    public void onClick(View v) {
        shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("Text/plain");
        shareIntent.setType("vnd.android-dir/mms-sms");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"my app");
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,msg);
        startActivity(Intent.createChooser(shareIntent,"shareVia"));

    }

    public void smsSend(View view){
        sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", "hello");
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(Intent.createChooser(sendIntent,"shareVia"));
    }
}

1 个答案:

答案 0 :(得分:0)

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

/**
 * Created by daffodil on 11/7/17.
 */
public class ShareIntentActivity  extends AppCompatActivity implements View.OnClickListener{

    Button sharebtn,smsbtn;
    String msg = "Welcome to Share app";  
    Intent shareIntent,sendIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_intent);
        sharebtn = (Button) findViewById(R.id.button);
        smsbtn = (Button) findViewById(R.id.smsbtn);
        sharebtn.setOnClickListener(this);
        smsbtn.setOnClickListener(this);
    }

    public void external_permissions(){

        if(ContextCompat.checkSelfPermission(this,
                Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.SEND_SMS}, 1);
        }
        else{
            sharebtn.setOnClickListener(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
            sharebtn.setOnClickListener(this);
        }
    }
    @Override
    public void onClick(View v) {
        external_permissions();
        switch(v.getId()){
            case R.id.button:
                shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("Text/plain");
                shareIntent.setType("vnd.android-dir/mms-sms");
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"my app");
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,msg);
                startActivity(Intent.createChooser(shareIntent,"shareVia"));
                break;

            case R.id.smsbtn:
                smsSend(v);
                break;
        }
    }

    public void smsSend(View view){
        sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", "hello");
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(Intent.createChooser(sendIntent,"shareVia"));
    }
}