Activity 基本使用
大约 3 分钟
Activity 基本使用
向另一个应用发送数据
- 发起通话
Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
- 查看网页
Uri webpage = Uri.parse("https://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
- 发送带有附件的电子邮件
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// 这个 intent 没有 URI, 所以要申明 "text/plain" 类型
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jan@example.com"}); //
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
- 创建日历活动
// 事件是在January 23, 2021 -- from 7:30 AM to 10:30 AM.
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2021, 0, 23, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2021, 0, 23, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Ninja class");
calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");
- 创建 intent 来查看地图,验证是否存在可以处理该 intent 的应用,然后启动它
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
try {
startActivity(mapIntent);
} catch (ActivityNotFoundException e) {
// 如果没有对应的应用可以处理
}
- 显示应用选择器
Intent intent = new Intent(Intent.ACTION_SEND);
String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent, title);
try {
startActivity(chooser);
} catch (ActivityNotFoundException e) {
// 如果没有对应的应用可以处理
}
更多示例可以访问官方文档:官方文档地址
获取Activity的结果的最佳做法
- 直接在Activity 内部使用
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
处理结果
}
});
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Button selectButton = findViewById(R.id.select_button);
selectButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mGetContent.launch("image/*");
}
});
}
- 封装到能够感知生命周期的类中(强烈推荐)
class MyLifecycleObserver implements DefaultLifecycleObserver {
private final ActivityResultRegistry mRegistry;
private ActivityResultLauncher<String> mGetContent;
MyLifecycleObserver(@NonNull ActivityResultRegistry registry) {
mRegistry = registry;
}
public void onCreate(@NonNull LifecycleOwner owner) {
mGetContent = mRegistry.register(“key”, owner, new GetContent(),
new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri uri) {
// 处理结果
}
});
}
public void selectImage() {
// 打开图片,选择图片
mGetContent.launch("image/*");
}
}
class MyFragment extends Fragment {
private MyLifecycleObserver mObserver;
@Override
void onCreate(Bundle savedInstanceState) {
mObserver = new MyLifecycleObserver(requireActivity().getActivityResultRegistry());
getLifecycle().addObserver(mObserver);
}
@Override
void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Button selectButton = findViewById(R.id.select_button);
selectButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mObserver.selectImage();
}
});
}
}
- 通用协定写法
ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
处理结果
}
}
});
@Override
public void onCreate(@Nullable savedInstanceState: Bundle) {
// ...
Button startButton = findViewById(R.id.start_button);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// 指定跳转到某个页面处理事务,返回数据写在intent上
mStartForResult.launch(new Intent(this, ResultProducingActivity.class));
}
});
}
如何让其他应用分享数据时能拉起你的应用进行操作
- 添加 intent 过滤器
// 数据类型为文本或图像时处理 ACTION_SEND intent
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
// 可以指定多个过滤器
<intent-filter>
<action android:name="android.intent.action.SENDTO"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
</activity>
- 处理其他应用发过来的数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
Uri data = intent.getData();
if (intent.getType().indexOf("image/") != -1) {
// do something
} else if (intent.getType().equals("text/plain")) {
// do something
}
}
- 返回一些结果
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri"));
setResult(Activity.RESULT_OK, result);
finish();
// 也可以直接指定结果值
setResult(RESULT_COLOR_RED);
finish();
在与其他应用交互时,软件包的可见性
关于一些特定交互相关,另写篇章阐述
Loading...