下面列出了android.content.Intent#setType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
txtSubject=(TextView)findViewById(R.id.result);
if (id == R.id.action_settings) {
Intent share=new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
String shareBody=txtSubject.getText().toString()+"\n\nhttps://evilinsult.com/";
String shareSubject=txtSubject.getText().toString();
share.putExtra(Intent.EXTRA_SUBJECT,shareSubject);
share.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(Intent.createChooser(share,"Share using"));
}
return super.onOptionsItemSelected(item);
}
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, !TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, "activity not found");
}
}
private void doSendIntent(final String subject, final String text) {
final Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
cordova.getActivity().startActivityForResult(sendIntent, 0);
}
/**
* Share plain text using system share
* @param context Context to share from
* @param text plain text to share
* @see Intent#ACTION_SEND
*/
public static void share(final Context context, final String text){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(shareIntent);
}
public static void shareURL(Context context,String title, String url){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,title+": "+url);
sendIntent.setType("text/plain");
Intent clipboardIntent = new Intent(context, CopyToClipboardActivity.class);
clipboardIntent.setData(Uri.parse(url));
Intent chooserIntent = Intent.createChooser(sendIntent,context.getString(R.string.share_with));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent });
context.startActivity(chooserIntent);
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (mContext.get() == null) return;
if (((AppCompatActivity) mContext.get()).isFinishing()) return;
mDialog.dismiss();
if (aBoolean) {
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL,
new String[]{mContext.get().getResources().getString(R.string.dev_email)});
intent.putExtra(Intent.EXTRA_SUBJECT,
"Report Bugs " + (mContext.get().getString(
R.string.app_name)));
intent.putExtra(Intent.EXTRA_TEXT, mStringBuilder.toString());
if (mZipPath != null) {
File zip = new File(mZipPath);
if (zip.exists()) {
Uri uri = getUriFromFile(mContext.get(), mContext.get().getPackageName(), zip);
if (uri == null) uri = Uri.fromFile(zip);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
mContext.get().startActivity(Intent.createChooser(intent,
mContext.get().getResources().getString(R.string.email_client)));
} else {
Toast.makeText(mContext.get(), R.string.report_bugs_failed,
Toast.LENGTH_LONG).show();
}
mZipPath = null;
}
@OnClick(R.id.send_btn)
void sendPacket(View view) {
String url = sendOutBean.getUrl();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "share to"));
}
private static Intent getIntentInvite(Context context) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.msg_try) + "\n\nhttps://www.netguard.me/\n\n");
return intent;
}
public void share(Context context, String url) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setType("text/plain");
context.startActivity(intent);
}
/**
* Shares a URL to the system.
*
* @param url the URL to share. If the URL is null
* or a special URL, no sharing will occur.
* @param title the title of the URL to share. This
* is optional.
*/
public void shareUrl(@Nullable String url, @Nullable String title) {
if (url != null && !UrlUtils.isSpecialUrl(url)) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
if (title != null) {
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
}
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
mActivity.startActivity(Intent.createChooser(shareIntent, mActivity.getString(R.string.dialog_title_share)));
}
}
/**
* Share text with given mime-type
*
* @param text The text to share
* @param mimeType MimeType or null (uses text/plain)
*/
public void shareText(final String text, @Nullable final String mimeType) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.setType(mimeType != null ? mimeType : MIME_TEXT_PLAIN);
showChooser(intent, null);
}
@Override
public void onReceive(Context context, Intent intent) {
String url = intent.getDataString();
if (url != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
Intent chooserIntent = Intent.createChooser(shareIntent, "Share url");
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooserIntent);
}
}
/**
* Creates an intent for an email activity
* @param address the email address to send to
* @param subject the subject
* @param body the body
* @param cc any email addresses to cc
* @return an intent that when started, prompts the user to send the email
*/
private static Intent newEmailIntent(String address, String subject, String body, String cc) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_CC, cc);
intent.setType("message/rfc822");
return intent;
}
public void pickPhoto(Activity activity, int requestCode) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
} else {
intent.setAction(Intent.ACTION_PICK);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
activity.startActivityForResult(intent, requestCode);
}
/**
* Call the system application with shares.
*
* @param context context
* @param title share title
* @param url share url
*/
public static void showShareMore(Activity context, final String title, final String url) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Share: " + title);
intent.putExtra(Intent.EXTRA_TEXT, title + " " + url);
context.startActivity(Intent.createChooser(intent, "Choose to share"));
}
public void onClick(View view) {
EditText editView = (EditText) findViewById(R.id.input);
String string = editView.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, string);
startActivity(Intent.createChooser(intent, "Share with:"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.share:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareText = getResources().getString(R.string.share_dialog_title_share);
String shareContent = getResources().getString(R.string.share_content);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareText);
shareIntent.putExtra(Intent.EXTRA_TEXT, url + shareContent);
startActivity(Intent.createChooser(shareIntent, shareText));
break;
case R.id.refresh:
webView.reload();
break;
case R.id.copy_link:
copyTextToClipboard();
break;
case R.id.open_in_browser:
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openBrowserIntent);
break;
}
return super.onOptionsItemSelected(item);
}
public static void shareUrl(Context context, String url) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "来自开源实验室的分享:" + url);
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, "发送到:"));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// Go to Set Alarm Activity
case R.id.im_set_alarm:
Intent intent = new Intent(MainActivity.this, SetAlarmActivity.class);
intent.putExtra(AlarmScheduler.X_ALARM_ID, mAlarm.getId());
startActivityForResult(intent, SET_ALARM_REQUEST_CODE);
break;
case R.id.iv_top_main_content_indicator:
if (mAlarm.isEnabled()) {
mAlarm.setEnabled(false);
mAlarm.cancel();
ivMainContentIndicator.setImageResource(R.drawable.main_mid_off);
ToastMaster.setToast(Toast.makeText(MainActivity.this,
getString(R.string.turn_off_alarm),
Toast.LENGTH_SHORT));
} else {
mAlarm.setEnabled(true);
mAlarm.schedule();
ivMainContentIndicator.setImageResource(R.drawable.main_mid);
ToastMaster.setToast(Toast.makeText(MainActivity.this,
getString(R.string.turn_on_alarm),
Toast.LENGTH_SHORT));
}
updateAlarmDistanceText();
ToastMaster.showToast();
break;
case R.id.iv_left_menu_indicator:
if (loopXDragMenuLayout.getMenuStatus() == DragMenuLayout.MenuStatus.Close) {
loopXDragMenuLayout.openLeftMenuWithAnimation();
} else {
loopXDragMenuLayout.closeMenuWithAnimation();
}
break;
case R.id.iv_right_menu_indicator:
if (loopXDragMenuLayout.getMenuStatus() == DragMenuLayout.MenuStatus.Close) {
loopXDragMenuLayout.openRightMenuWithAnimation();
} else {
loopXDragMenuLayout.closeMenuWithAnimation();
}
break;
case R.id.btn_about:
Intent intentToAbout = new Intent(this, AboutActivity.class);
startActivity(intentToAbout);
break;
case R.id.btn_share:
Intent intentToShare = new Intent(Intent.ACTION_SEND);
intentToShare.setType("text/plain");
intentToShare.putExtra(Intent.EXTRA_SUBJECT, "X Alarm");
String sAux = "\n独特的起床闹钟\n\n";
// ToDo 分享
sAux = sAux + "XXXXX \n\n";
intentToShare.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(intentToShare, "Choose one"));
break;
}
}
/**
* Pick file from sdcard with file manager. Chosen file can be obtained from Intent in onActivityResult.
* See code below for example:
* <p/>
* <pre><code>
* @Override
* protected void onActivityResult(int requestCode, int resultCode, Intent data) {
* Uri file = data.getData();
* }
* </code></pre>
*/
public static Intent newPickFileIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
return intent;
}