溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Android 開發(fā)中使用Activity怎么實現(xiàn)一個隱式跳轉(zhuǎn)功能

發(fā)布時間:2020-11-21 16:32:07 來源:億速云 閱讀:244 作者:Leah 欄目:移動開發(fā)

在Android 開發(fā)中使用Activity怎么實現(xiàn)一個隱式跳轉(zhuǎn)功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)建第二個activity就是創(chuàng)建一個class,繼承自Android.app.Activity.

創(chuàng)建第二個activity的同時需要在清單文件中配置,不然會找不到

<activity android:name="com.ldw.createActivity.SecondActivity"></activity> 

入口activity有下面的代碼,只要activity有下面的代碼,就會創(chuàng)建一個圖標。默認圖標是一樣的

可以通過android:lable=“”來設置圖標的名字。

<intent-filter> 
  <action android:name="android.intent.action.MAIN" /> 
  <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 

如果activity所在的包跟應用包名同名,則可以不寫。

完整的清單中的配置如下:

<activity 
 android:name="com.ldw.activityto.MainActivity" 
 android:label="@string/app_name" > 
 <intent-filter> 
  <action android:name="android.intent.action.MAIN" /> 
 
  <category android:name="android.intent.category.LAUNCHER" /> 
 </intent-filter> 
</activity> 

隱式跳轉(zhuǎn)和顯示跳轉(zhuǎn)

顯示跳轉(zhuǎn)到activity

顯示跳轉(zhuǎn)中清單文件需要添加下面的配置

<activity android:name="com.ldw.activityto.SecondActivity"></activity> 

代碼中的實現(xiàn)如下

/* 
 * 跳轉(zhuǎn)到本應用中的activity 
 * 顯示跳轉(zhuǎn):直接指定目標activity的包名和類名 
 */ 
public void click2(View v){ 
 Intent intent = new Intent(); 
 //第一個參數(shù)是上下文對象,第二個參數(shù)是制定目的activity的類名 
 //顯示意圖 
 intent.setClass(this, SecondActivity.class); 
 startActivity(intent); 
}

隱式跳轉(zhuǎn)到activity

intent-filter意圖過濾器中有3個參數(shù)action,category,data。action和data可以配置多個。category是系統(tǒng)的配置,action中的name是自己隨便定義的,定義好以后name的值就是activity的動作,隱式啟動activity時,意圖中的配置必須和這里的action的name是一致的。data是跳轉(zhuǎn)的過程中攜帶的參數(shù),mimeType是攜帶的數(shù)據(jù)的類型,根據(jù)意圖過濾器中中的配置,跳轉(zhuǎn)中針對data的配置需要做不同的處理。    

<activity android:name="com.ldw.activityto.SecondActivity"> 
   <intent-filter> 
    <action android:name="com.ldw.activityto.sa"/> 
<span >  </span> <action android:name="com.ldw.activityto.sasa"/> 
<span >  </span> <data android:scheme="ldw" android:mimeType="text/password"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
   </intent-filter> 
  </activity> 

代碼中的實現(xiàn)如下      

/* 
  * 隱式跳轉(zhuǎn)到撥secondActivity 
  */ 
 public void click5(View v){ 
 <span > </span>Intent intent = new Intent(); 
 <span > </span>//目標activity的包名和類名 
 <span > </span>intent.setAction("com.ldw.activityto.sa"); 
 <span > </span>intent.setData(Uri.parse("ldw:canshu")); //scheme中的參數(shù)加上冒號,沒有miniType時候的配置 
 <span > </span>//intent.setType("text/password");//沒有配置data卻有miniType的時候的配置 
 <span > </span>//intent.setDataAndType(Uri.parse("ldw:canshu"), "text/password");//data和miniType都有的時候的 
 <span > </span>intent.addCategory(Intent.CATEGORY_DEFAULT);//不寫這句系統(tǒng)就添加默認的category 
 <span > </span>startActivity(intent); 
 } 

activity中獲取到傳遞的參數(shù)的方法:

package com.ldw.activityto; 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
public class SecondActivity extends Activity { 
 @Override 
 protected void onCreate(Bundle savedInstanceState){ 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_second); 
  //獲取到啟動這個activity的意圖 
  Intent intent = getIntent(); 
  //獲取到傳遞過來的數(shù)據(jù) 
  Uri uri = intent.getData(); 
 } 
} 

如何選擇哪一種啟動方式:啟動同一個應用中的activity適合用顯示,啟動不同應用中的activiy適合用隱式。全部使用隱式是完全沒有問題的,使用顯示的效率更高一些。當系統(tǒng)中有多個activity與意圖設置的Action匹配,那么啟動Activity時,會彈出對話框,里面包含匹配的Activity。

打電話應用的配置 

/* 
 * 跳轉(zhuǎn)到打電話activity 
 * 隱式跳轉(zhuǎn):通過制定action和data來跳轉(zhuǎn) 
 */ 
 public void click1(View v){ 
 Intent intent = new Intent(); 
 //隱式意圖 
 intent.setAction(Intent.ACTION_CALL); 
 intent.setData(Uri.parse("tel:1190")); 
 //跳轉(zhuǎn) 
 startActivity(intent); 
 } 
 /* 
 * 顯示跳轉(zhuǎn)到撥號器 
 */ 
 public void click3(View v){ 
 Intent intent = new Intent(); 
 //目標activity的包名和類名 
 intent.setClassName("com.android.dialer", ".DialtactsActivity"); 
 startActivity(intent); 
 } 

啟動瀏覽器的方式  

/* 
  * 顯示跳轉(zhuǎn)到瀏覽器 
  */ 
 public void click6(View v){ 
 <span > </span>Intent intent = new Intent(); 
 <span > </span>//目標activity的包名和類名 
 <span > </span>intent.setClassName("com.android.browser","com.android.browser.BrowserActivity"); 
 <span > </span>startActivity(intent); 
 } 
 /* 
  * 隱式跳轉(zhuǎn)到瀏覽器 
  */ 
 public void click7(View v){ 
 <span > </span>Intent intent = new Intent(); 
 <span > </span>//目標activity的包名和類名 
 <span > </span>intent.setAction(intent.ACTION_VIEW); 
 <span > </span>intent.setData(Uri.parse("http://www.baidu.com")); 
 <span > </span>startActivity(intent); 
 } 

看完上述內(nèi)容,你們掌握在Android 開發(fā)中使用Activity怎么實現(xiàn)一個隱式跳轉(zhuǎn)功能的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI