Android Intent的 Component 使用
生活随笔
收集整理的這篇文章主要介紹了
Android Intent的 Component 使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Intent 組成元素的列表說明
| 元素名稱 | ?設置方法 | ?說明與用途 |
| Component?? ? | setComponent?? ? | 組件,用于指定Intent的來源與目的 |
| Action?? ? | setAction?? ? | 動作,用于指定Intent的操作行為 |
| Data | setData?? ? | ?即Uri,用于指定動作要操縱的數據路徑 |
| Category?? ? | setCategory?? ? | 類別,用于指定Intent的操作類別 |
| Type?? ? | setType?? ? | 數據類型,用于指定Data類型的定義 |
| Extras?? ? | setExtras | 擴展信息,用于指定裝載的參數信息 |
| Flags | setFlags?? ? | ?b標志位,用于指定Intent的運行模式(啟動標志)? |
Intent?設置組件名稱的方法:setComponent()、setClass()、setClassName() 或 Intent 構造函數
這里回顧下setComponent() 方法,
里面有三個方法如下
第一個?創建一個新的組件標識符
通過注釋可以看出這個方法的作用:創建一個新的組件標識符
參數pkg :組件所在包的名稱,不能為空(可以把它看作applicationId)
就是這個
參數cls:pkg 中實現組件的類的名稱,不能為空 (可以想成是包名和類型的組合)
下面實現下
Intent intent1 = new Intent();ComponentName componentName = new ComponentName("com.example.myapplication","com.example.myapplication.SecondActivity");intent1.setComponent(componentName);startActivity(intent1);
第二個:從上下文和類名創建一個新的組件標識符
從注釋可以看出這個方法的作用:從上下文和類名創建一個新的組件標識符
參數pkg:實現組件的包的上下文,從中檢索實際包名稱。(上下文)
參數cls:<var>pkg</var> 里面的類名,實現組件(報名+類名)
代碼如下:
Intent intent1 = new Intent();ComponentName componentName = new ComponentName(MainActivity.this,"com.example.myapplication.SecondActivity");intent1.setComponent(componentName);startActivity(intent1);
第三個:從上下文和類名創建一個新的組件標識符
參數pkg:pkg 實現組件的包的上下文,從中檢索實際包名稱
參數cls: <var>pkg</var> 里面的類名實現組件
實現代碼:
Intent intent1 = new Intent();ComponentName componentName = new ComponentName(MainActivity.this,SecondActivity.class);intent1.setComponent(componentName);startActivity(intent1);
?? ??
??
?? ?
??
總結
以上是生活随笔為你收集整理的Android Intent的 Component 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HarmonyOS ListContai
- 下一篇: Android Intent setAc