一、通过显式意图跳转,如字面意思一样,跳转的目的地是可以明确看到的
二、通过隐式意图跳转,这种方式的跳转需要我们在AndroidManifest.xml文件中为目的Activity配置Action和Category
模拟账号登录,分别使用俩种方式跳转
这是启动的Activity
package com.example.myapplication.intentinner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.R;
public class SendMessageActivity extends AppCompatActivity {
private Button buttonShow;
private Button buttonHide;
private EditText editTextAccount;
private EditText editTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
buttonShow = findViewById(R.id.btn_send_show);
buttonHide = findViewById(R.id.btn_send_hide);
editTextAccount = findViewById(R.id.edit_text_account);
editTextPassword = findViewById(R.id.edit_text_password);
}
private void initListener() {
//通过显式意图跳转
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SendMessageActivity.this, ReceiverShowActivity.class);
String account = editTextAccount.getText().toString().trim();
if(TextUtils.isEmpty(account)){
Toast.makeText(SendMessageActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
String password = editTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(password)){
Toast.makeText(SendMessageActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
}
});
//通过隐式意图跳转,需要添加action和category,在AndroidManifest.xml文件对应的Activity中查看
buttonHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.myapplication.LOGIN");
intent.addCategory(Intent.CATEGORY_DEFAULT);
String account = editTextAccount.getText().toString().trim();
if(TextUtils.isEmpty(account)){
Toast.makeText(SendMessageActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
String password = editTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(password)){
Toast.makeText(SendMessageActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
}
});
}
}
下面是显示意图的目的Activity
package com.example.myapplication.intentinner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.example.myapplication.R;
public class ReceiverShowActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver_show);
//通过getIntent()方法获取传过来Intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password");
textView = findViewById(R.id.text_view_show_result);
textView.setText("账号为:"+account+",密码为:"+password);
}
}
下面是隐式意图的目的Activity
package com.example.myapplication.intentinner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.myapplication.R;
public class ReceiverHideActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver_hide);
//通过getIntent()方法获取传过来Intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password");
textView = findViewById(R.id.text_view_hide_result);
textView.setText("账号为:"+account+",密码为:"+password);
}
}
跳转到第三方应用,例如手机的浏览器,电话
打开手机的浏览器
package com.example.myapplication.intentout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.example.myapplication.R;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
public void skipShowIntent(View view){
Intent intent = new Intent();
//这里举个例子,因为不同机型的浏览器包名可能不同,请根据具体情况而定
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);
}
public void skipHideIntent(View view){
Intent intent = new Intent();
//这里举个例子,Action、Category和Package视情况而定
intent.setAction("android.intent.action.VIEW");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}
跳转后获取数据返回
在第二个界面输入内容后返回到第一个界面并显示
package com.example.myapplication.intentforresult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.myapplication.R;
public class RequestActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);
textView = findViewById(R.id.text_view_request);
button = findViewById(R.id.btn_for_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RequestActivity.this,ResponseActivity.class);
startActivityForResult(intent,1);
}
});
}
//获取请求结果
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String msg = data.getStringExtra("result");
if(1 == requestCode){
if(2 == resultCode){
textView.setText(msg);
}
}
}
}
输入数据并返回界面
package com.example.myapplication.intentforresult;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.R;
public class ResponseActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_response);
editText = findViewById(R.id.edit_text_response);
button = findViewById(R.id.btn_back);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = editText.getText().toString();
if (TextUtils.isEmpty(msg)) {
Toast.makeText(ResponseActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent();
intent.putExtra("result",msg);
//设置结果码和数据
setResult(2,intent);
//不要忘了调用finish()方法结束当前Activity
finish();
}
});
}
}
这有俩个例子分别是打电话和照相
打电话是跳转第三方应用,照相不仅调用第三方应用,还用到了数据回传
下面是打电话的代码
package com.example.myapplication.intentwithoperate;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.example.myapplication.R;
public class CallPhoneActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_phone);
}
public void callPhone(View view){
Intent intent = new Intent();
Uri uri = Uri.parse("tel:10086");
intent.setData(uri);
intent.setAction("android.intent.action.CALL");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}
下面是拍照,注意在虚拟机上无法拍照,所以看不到效果,我在真机上测试没有问题的
package com.example.myapplication.intentwithoperate;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.myapplication.R;
public class CameraRequestActivity extends AppCompatActivity {
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_request);
button = findViewById(R.id.btn_camera);
imageView = findViewById(R.id.image_camera);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(1 == requestCode){
if(Activity.RESULT_OK == resultCode && data != null){
//用Bitmap来接收照片
Bitmap bitmap = data.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);
}else if(Activity.RESULT_CANCELED == resultCode){
Toast.makeText(this,"失败",Toast.LENGTH_SHORT).show();
}
}
}
}
-----仅为自己的学习笔记
作者:Silence智鹏