網(wǎng)站首頁 編程語言 正文
Intent
將Activity 、Serivce 、 BroadReceive 之間通信 使用Intent
Intent 對象屬性:
- 1.Action
- 2. Data
- 3.Category
- 4. Extras
- 5.Flags
- 6.Component name
Component name:
設(shè)置Intnet 組件對象 名稱 通過 包名 + 類名 確定唯一的一個activity
類似Spring 中Component 根據(jù)component name 設(shè)置或者啟動特定的組件
setComponent(componentName)
Intnet intnet = new Intnet();
ComponentName componentName = new ComponentName("com.example","com.example.DetailActivity");
intnet.setComponent(componentName);
startActivity(intnet)
Action & Data
接下來動作 和 對應(yīng)數(shù)據(jù)
Action 屬性和Data 屬性
第一步在 manifest 設(shè)置開啟電話和郵件的權(quán)限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 自定義Activity 需要在mainifests 配置聲明 --> </application> </manifest>
package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
/**
* 功能: Action Data 共用 點擊打電話 和 發(fā)送郵件按鈕 進行 跳轉(zhuǎn)系統(tǒng) 打電話 和郵件界面
*
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton phoneButton = findViewById(R.id.imageButton_phone);
ImageButton smsButton = findViewById(R.id.imageButton_sms);
phoneButton.setOnClickListener(l);
smsButton.setOnClickListener(l);
}
// 定義監(jiān)聽器對象 方法共用
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
ImageButton imageButton = (ImageButton) view;
switch (imageButton.getId()){
case R.id.imageButton_phone:
// 調(diào)用撥號面板
intent.setAction(intent.ACTION_DIAL);
intent.setData(Uri.parse("tel: 043184978981"));
startActivity(intent);
break;
case R.id.imageButton_sms:
intent.setAction(intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto: 5554"));
// 設(shè)置短信內(nèi)容
intent.putExtra("sms_body", "welcome to Android");
startActivity(intent);
break;
}
}
};
}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="技術(shù)支持:吉林省明日科技有限公司" android:layout_marginTop="20dp"/>
<TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="網(wǎng)址:http://www.mingrisoft.com" android:layout_marginTop="10dp" android:layout_below="@+id/text1"/> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="企業(yè)郵箱:mingrisoft@mingrisoft.com" android:layout_marginTop="10dp" android:layout_below="@+id/text2"/> <TextView android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="技術(shù)服務(wù)熱線:0431-84978981" android:layout_marginTop="10dp" android:layout_below="@+id/text3"/> <ImageButton android:id="@+id/imageButton_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/phone" android:layout_below="@+id/text4" android:layout_marginTop="30dp" android:background="#0000" android:scaleType="fitXY" /> <ImageButton android:id="@+id/imageButton_sms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageButton_phone" android:layout_below="@+id/text4" android:layout_marginTop="30dp" android:layout_marginLeft="30dp" android:background="#0000" android:scaleType="fitXY" android:src="@drawable/sms"/> </RelativeLayout>
Action & Category
Category屬性 將Activity 進行分類, 將對應(yīng)Activity 進行分類
package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
/**
* 功能: Action Data 共用 點擊打電話 和 發(fā)送郵件按鈕 進行 跳轉(zhuǎn)系統(tǒng) 打電話 和郵件界面
*
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設(shè)置全屏顯示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ImageButton imageButton= (ImageButton) findViewById(R.id.imageButton1); //獲取ImageView組件
//為ImageView組件設(shè)置單擊事件監(jiān)聽器
imageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(intent.ACTION_MAIN);
intent.addCategory(intent.CATEGORY_HOME);
}
});
}
}
Flags 屬性
作用: 讓當(dāng)前的activity 不在歷史棧當(dāng)中保留,當(dāng)用戶離開app 再一次進入時候 不在是當(dāng)前Activity 界面 轉(zhuǎn)到系統(tǒng)的主界面
Intnet 種類
顯示Intnet:
創(chuàng)建Intnet對象 -> 目標(biāo)組件 ->啟動 目標(biāo)組件:
Intnet intnet = new Intnet(MainActivity.this, 調(diào)用Activity對象)
隱式Intnet創(chuàng)建對象不指定 目標(biāo)組件,通過action , category , data 讓Android 系統(tǒng)自動匹配目標(biāo)組件
區(qū)別: 顯示 直接指定目標(biāo)組件名稱,多常用在應(yīng)用程序內(nèi)部傳遞信息
隱式Intnet:不會用組件名稱定義激活的目標(biāo)組件,多常用在不同應(yīng)用程序之間傳遞信息
Intent Filter
Activity A 通過category + action + data 作為Filter的條件刷選出來的ActivityB 是目標(biāo)Activity
在AndroidManifest.xml 文件中配置
<intent-filter> <category></category> <action></action> </intnet-filter> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
打開圖片時候 采用不同app畫圖 或者 點擊
通過隱式Intnet 完成
原文鏈接:https://blog.csdn.net/qq_27217017/article/details/106984507
相關(guān)推薦
- 2022-04-28 WPF依賴屬性用法詳解_實用技巧
- 2022-03-20 Android實現(xiàn)桌面快捷方式實例代碼_Android
- 2021-12-11 C++嵌入式內(nèi)存管理詳情_C 語言
- 2022-06-12 Python同步方法變?yōu)楫惒椒椒ǖ男〖记煞窒韄python
- 2022-12-27 C語言中strcpy()函數(shù)的具體實現(xiàn)及注意事項_C 語言
- 2021-12-05 C++11?關(guān)鍵字?const?使用小結(jié)_C 語言
- 2023-03-29 Pytorch中的數(shù)據(jù)轉(zhuǎn)換Transforms與DataLoader方式_python
- 2022-06-18 Python周期任務(wù)神器之Schedule模塊使用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支