안드로이드:인텐트(intent)를 사용하여 일반 텍스트 공유(모든 메시징 앱에)
다음과 같은 의도를 사용하여 텍스트를 공유하려고 합니다.
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");
선택자와 몸싸움을 벌입니다
startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));
작동합니다! 하지만 이메일 앱에 한해서만 가능합니다.
제가 필요한 것은 모든 메시징 앱에 대한 일반적인 의도입니다. 이메일, SMS, IM(Whatsapp, Viber, Gmail, SMS...)을 사용하려고 시도했습니다.android.content.Intent.ACTION_VIEW
사용해 보았습니다.i.setType("vnd.android-dir/mms-sms");
아무것도 도움이 안 됐어요...
("vnd.android-dir/mms-sms"
sms만 사용하여 공유합니다!)
코드는 다음과 같이 사용합니다.
/*Create an ACTION_SEND Intent*/
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
/*This will be the actual content you wish you share.*/
String shareBody = "Here is the share content body";
/*The type of the content is text, obviously.*/
intent.setType("text/plain");
/*Applying information Subject and Body.*/
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
/*Fire!*/
startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
새로운 방법은 ShareCompat을 사용하는 것입니다.IntentBuilder는 다음과 같습니다.
// Create and fire off our Intent in one fell swoop
ShareCompat.IntentBuilder
// getActivity() or activity field if within Fragment
.from(this)
// The text that will be shared
.setText(textToShare)
// most general text sharing MIME type
.setType("text/plain")
.setStream(uriToContentThatMatchesTheArgumentOfSetType)
/*
* [OPTIONAL] Designate a URI to share. Your type that
* is set above will have to match the type of data
* that your designating with this URI. Not sure
* exactly what happens if you don't do that, but
* let's not find out.
*
* For example, to share an image, you'd do the following:
* File imageFile = ...;
* Uri uriToImage = ...; // Convert the File to URI
* Intent shareImage = ShareCompat.IntentBuilder.from(activity)
* .setType("image/png")
* .setStream(uriToImage)
* .getIntent();
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email recipients as an array
* of Strings or a single String
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email addresses that will be
* BCC'd on an email as an array of Strings or a single String
*/
.addEmailBcc(arrayOfStringEmailAddresses)
.addEmailBcc(singleStringEmailAddress)
/*
* The title of the chooser that the system will show
* to allow the user to select an app
*/
.setChooserTitle(yourChooserTitle)
.startChooser();
ShareCompat 사용에 관해 더 궁금한 점이 있다면, Google의 Android Developer Advocator인 Ian Lake가 API를 좀 더 완벽하게 분석할 수 있도록 이 훌륭한 기사를 적극 추천합니다.당신이 알아차리겠지만, 저는 그 기사에서 이 예시의 일부를 빌렸습니다.
만약 그 기사가 당신의 모든 질문에 대답하지 않는다면, 쉐어컴뱃을 위한 JavaDoc 자체가 항상 존재합니다.Android Developers 웹사이트의 IntentBuilder.clemantiano의 코멘트를 바탕으로 API의 사용 예시를 추가했습니다.
Android에서 Intents와의 공유에 대한 좋은 예는 다음과 같습니다.
//Share text:
Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);
intent2.setType("text/plain");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(Intent.createChooser(intent2, "Share via"));
//via Email:
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("message/rfc822");
intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});
intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(intent2);
//Share Files:
//Image:
boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;
Intent i = new Intent(Intent.ACTION_SEND);
//Set type of file
if(isPNG)
{
i.setType("image/png");//With png image file or set "image/*" type
}
else
{
i.setType("image/jpeg");
}
Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of image
i.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of image
startActivity(Intent.createChooser(i, "Share via"));
break;
//APK:
File f = new File(path1);
if(f.exists())
{
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("application/vnd.android.package-archive");//APk file type
intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(Intent.createChooser(intent2, "Share via"));
}
break;
아래 방법을 사용하고 주제와 본문을 방법의 인수로 전달합니다.
public static void shareText(String subject,String body) {
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("text/plain");
txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
아래는 이메일이나 메시지 앱과 함께 작동하는 코드입니다.이메일로 공유하면 제목과 본문이 모두 추가됩니다.
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
"<p>Store Name:" + “store_name “+ "</p>" +
"<p>Store Address:" + “store_address” + "</p>") .toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
else {
Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
}
다음을 사용하여 의도를 작성합니다.ACTION_SEND
당신은 추가로 그것의 타입을 넣을 수 있을 것입니다.Intent.EXTRA_TEXT
, 두번째 인수는 공유하고 싶은 텍스트입니다.그런 다음 공유 유형을 다음과 같이 설정합니다.text/plain
, 인텐트 서비스는 텍스트 공유를 지원하는 모든 앱을 제공합니다.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
이미지 또는 이진 데이터:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
또는 HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
코틀린
클릭 청취자 안에서 왓츠앱, Gmail, 슬랙과 같은 애플리케이션을 통해 텍스트를 공유하기 위해 이 모듈을 추가해야 했습니다.
shareOptionClicked.setOnClickListener{
val shareText = Intent(Intent.ACTION_SEND)
shareText.type = "text/plain"
val dataToShare = "Message from my application"
shareText.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
shareText.putExtra(Intent.EXTRA_TEXT, dataToShare)
startActivity(Intent.createChooser(shareText, "Share Via"))
}
SMS를 통한 공유를 위한 코드입니다.
String smsBody="Sms Body";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Gmail 공유를 위한 100% 작동 코드
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
intent.setPackage("com.google.android.gm");
if (intent.resolveActivity(getPackageManager())!=null)
startActivity(intent);
else
Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();
언급URL : https://stackoverflow.com/questions/9948373/android-share-plain-text-using-intent-to-all-messaging-apps
'source' 카테고리의 다른 글
Flask @app.ro ute에서 python 콘솔로 인쇄하는 방법 (0) | 2023.09.21 |
---|---|
Go-SQL-Driver로 인해 maria-db CPU 사용률이 매우 높음 (0) | 2023.09.21 |
mysql의 기본 포트를 3306에서 3360으로 변경하는 방법 (0) | 2023.09.16 |
PL/SQL을 사용하여 Excel 파일(.xlsx) 만들기 (0) | 2023.09.16 |
MariaDB - sql 쿼리 성능 또는 최적화 (0) | 2023.09.16 |