網站首頁 編程語言 正文
概述
Android開發少不了的就是頁面之間的跳轉,或者想要呼叫打開其他應用等
Intent
Intent是Android程序中各組件之間進行交互的一種重要方式,不僅可以指明當前組件想要執行的運作,還可以在不同組件之間傳遞數據。
顯示Intent啟動
第一個參數為啟動活動的上下文
第二個參數為想要啟動的目標活動
Intent intent = new Intent(MainActivity.this, TabHostActivity.class); startActivity(intent);
通過這個構造函數就可以構建出Intent的“意圖”,且目標明確,所以為顯示啟動
隱式Intent啟動
根據 <intent-filter>
中設定的 action 和 category 來啟動,且只有<action>和<category>
中的內容同時能夠匹配上時,這個活動才能響應。
Intent intent = new Intent("com.zqunyan.zgstudy.ACTION_START"); intent.addCategory("com.zqunyan.zgstudy.MY_CATEGORY"); startActivity(intent);
如果 <intent-filter>
中的 category
值是 android.intent.category.DEFAULT
則可以省略addCategory(),因為DEFAULT是一種默認的 category,在調用 startActivity() 方法的時候會自動添加到 intent 中,即
Intent intent = new Intent("com.zqunyan.zgstudy.ACTION_START"); startActivity(intent);
啟動其他程序
使用隱式 Intent,我們不僅可以啟動自己程序內的活動,還可以啟動其他程序的活動。
網頁瀏覽
action指定為Intent.ACTION_VIEW
,其常量值為 android.intent.action.VIEW
然后將網頁地址轉換成 Uri 對象傳遞進去
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.baidu.com")); startActivity(intent);
撥號界面
action指定為 Intent.ACTION_DIAL
Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent);
根據包名打開軟件
借助androdi內部的 PackageManager 來根據包名取得應用的啟動頁面
Intent intent = getPackageManager().getLaunchIntentForPackage("com.zqunyan.zgwidget"); if(intent != null){ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
根據類名打開界面
常用于打開系統設置界面,用于一些快捷功能設置。借助 Component 來實現
如:下面實例打開華為手機的設置頁面
Intent intent = new Intent(); ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.Settings$DisplaySettingsActivity"); intent.setComponent(componentName); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
startActivityForResult
主頁面
跳轉按鈕點擊事件
Intent intent = new Intent(MainActivity.this, ReturnValueActivity.class); //第二個參數用于處理返回結果是判斷是哪個語句調用的 startActivityForResult(intent, 1); //requestCode = 1
處理返回結果
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent){ super.onActivityResult(requestCode, resultCode, intent); if(requestCode == 1){ if(resultCode == 1){ String content = intent.getStringExtra("content"); lblStatus.setText("success"); }else{ lblStatus.setText("fail"); } } }
跳轉界面
跳轉界面傳回返回值,并關閉界面
實例化一個空的 Intent綁定數據到 IntentsetResult() 回傳結果值和綁定了數據的 Intent關閉自身,主畫面接收返回結果
Intent intent = new Intent(); intent.putExtra("content", txtMessage.getText().toString()); //setResult第一個參數為結果碼,常用的有Activity.RESULT_OK、RESULT_CANCELED或者自定義整數型結果碼 // 第二個參數為返回值,返回值封裝在Intent中 setResult(1, intent); finish();
頁面傳值
Intent.putExtra 傳值
傳值
intent.putExtra("uname", "admin");
取值
Intent intent = getIntent(); String name = intent.getStringExtra("uname")
借助 Bundle 傳值
用于在 Activity 之間傳送值或數組資料,好處是當一個頁面跳轉多個頁面時,可以共用bundle。
簡單值
打包
Bundle bundle = new Bundle(); bundle.putString("NAME", "QY"); bundle.putInt("AGE", 18); bundle.putDouble("TALL", 175.86); bundle.putStringArrayList("lstFilePaths", lstFilePaths); intent.putExtras(bundle);
收包
Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); String strName = bundle.getString("NAME"); int intAge = bundle.getInt("AGE"); ArrayList<String> lstFilePaths = bundle.getStringArrayList("lstFilePaths");
StringArrayList
正常傳值都為單個實際值,如果想要傳送列表數據則一般通過構造函數或參數傳遞,直接通過 bundle 只能傳送 StringArrayList 簡單列表類型
自定義數據類
首先將自定義的數據類序列化,即繼承 implements Serializable 接口
數據類
public class GoodsInfoModule implements Serializable {}
傳值
bundle.putSerializable("goodsInfo", goodsInfo);
取值
goodsInfo = (GoodsInfoModule) bundle.getSerializable("goodsInfo");
原文鏈接:https://blog.csdn.net/ymtianyu/article/details/121789059
相關推薦
- 2022-04-03 詳解iOS?實現一對多代理方案_IOS
- 2022-12-22 使用PyCharm調試程序實現過程_python
- 2022-04-10 解析React?中的Virtual?DOM_MsSql
- 2023-02-10 C/C++開發中extern的一些使用注意事項_C 語言
- 2022-06-29 Python解決非線性規劃中經濟調度問題_python
- 2022-09-05 python使用pip成功導入庫后還是報錯的解決方法(針對vscode)_python
- 2022-06-22 關于Pycharm配置翻譯插件Translation報錯更新TTK失敗不能使用的問題_python
- 2022-05-07 .NET?Core中簡單的郵箱格式校驗方式_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支