通常,对于 Android 中的常见任务,您会发送一个通用 Intent,其他应用程序可以在该 Intent 上注册。
例如,要分享一些文本,您将创建如下意图:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
将提示 android 的本机共享对话框,用户可以在该对话框中选择他想如何共享它。
特别是对于电子邮件,您会执行以下操作:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","email@domain.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "This is my email subject");
startActivity(Intent.createChooser(intent, "Email"));
其他示例可能是启动默认短信应用程序:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", getMessageBody());
或者打开手机的拨号器:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
你需要弄清楚你想在你的应用程序中实现哪些操作,然后弄清楚如何实现它们中的每一个。
您可以在此处找到更多数据: