應用內Activity的跳轉方式
一、通過顯式意圖跳轉,如字面意思一樣,跳轉的目的地是可以明確看到的
二、通過隱式意圖跳轉,這種方式的跳轉需要我們在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();
}
/**
* 初始化UI
*/
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);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".intentinner.SendMessageActivity">
<EditText
android:id="@+id/edit_text_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="16"
android:inputType="text"
android:digits="qwertyuiopasdfghjklzxcvbnm."
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="20"
android:inputType="textPassword"
android:digits="1234567890."
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_send_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯式意圖跳轉"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_send_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隱式意圖跳轉"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
下面是顯示意圖的目的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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄成功"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="@+id/text_view_show_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@color/colorPrimaryDark"
android:layout_marginTop="15dp"/>
</LinearLayout>
下面是隱式意圖的目的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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄成功"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="@+id/text_view_hide_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@color/colorPrimaryDark"
android:layout_marginTop="15dp"/>
</LinearLayout>
跳轉到第三方應用,例如手機的瀏覽器,電話
打開手機的瀏覽器
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);
}
/**
* 顯式意圖跳轉手機瀏覽器
* @param view
*/
public void skipShowIntent(View view){
Intent intent = new Intent();
//這裏舉個例子,因爲不同機型的瀏覽器包名可能不同,請根據具體情況而定
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);
}
/**
* 隱式意圖跳轉手機瀏覽器
* @param view
*/
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯式意圖跳轉"
android:onClick="skipShowIntent"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="隱式意圖跳轉"
android:onClick="skipHideIntent"
android:textSize="20sp"/>
</LinearLayout>
跳轉後獲取數據返回
在第二個界面輸入內容後返回到第一個界面並顯示
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);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<Button
android:id="@+id/btn_for_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="請求數據"/>
</LinearLayout>
輸入數據並返回界面
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();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit_text_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入內容"
android:textSize="20dp"/>
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回結果"/>
</LinearLayout>
這有倆個例子分別是打電話和照相
打電話是跳轉第三方應用,照相不僅調用第三方應用,還用到了數據回傳
下面是打電話的代碼
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="撥打電話"
android:onClick="callPhone"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
下面是拍照,注意在虛擬機上無法拍照,所以看不到效果,我在真機上測試沒有問題的
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);
/**
* Activity.RESULT_OK和Activity.RESULT_CANCELED是安卓系統定義的倆個常量,用於表示相機照相的“√”和“×”
*/
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();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".intentwithoperate.CameraRequestActivity">
<Button
android:id="@+id/btn_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打開相機"
android:textSize="20sp"/>
<ImageView
android:id="@+id/image_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
-----僅爲自己的學習筆記