<span role="heading" aria-level="2">Activity组件(二):通过显式意图和隐式意图来跳转至第三方应用</span>

一、显式意图来跳转到第三方应用

   /**                                                                                                                                                              
    * 这个方***在点击按钮的时候执行                                                                                                                                               
    * @param view                                                                                                                                                   
    */                                                                                                                                                              
                                                                                                                                                                    
   public void skip2Browser(View view){                                                                                                                             
                                                                                                                                                                    
       Log.d(TAG,"skp2Browser");                                                                                                                                    
       Intent intent =new Intent();                                                                                                                                 
       /**                                                                                                                                                          
        * 組件的名称,也就是ComponentName =包名/类的路径名称,如包名一直,类的路径名称可以省略包名                                                                                                      
        * 如:com.google.android.as/com.google.android.apps.miphone.aiai.app.AiAiPredictionService                                                                    
        */                                                                                                                                                          
       //第一种写法                                                                                                                                                      
       // intent.setClassName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");                                            
                                                                                                                                                                    
        //第二种写法                                                                                                                                                     
       ComponentName  componentName=new ComponentName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");                    
       intent.setComponent(componentName);                                                                                                                          
                                                                                                                                                                    
       startActivity(intent);                                                                                                                                       
   }                                                                                                                                                                

 

 

 

 

 

 

 

 

 

 点击按钮

 

 

 

 

 

 实现跳转

二、隐式意图来跳转到第三方应用

 /**                                                               
  * 下面通过隐式意图来跳转到第三方应用                                              
  * 1、创建Intent对象                                                   
  * 2、第二步给intent对象,设置Action、category,5.1以上要设置包名                    
  * 3、startActivity(intent)                                        
  * @param view                                                    
  */                                                               
     public void skip2BrowserInvisible(View view) {                
         Intent intent =new Intent();                              
         intent.setAction("android.intent.action.SEARCH");         
         intent.addCategory("android.intent.category.DEFAULT");    
         intent.setPackage("com.google.android.as");               
         startActivity(intent);                                    
     }                                                             

 

 

 

 

 

 

activity_skip2browser.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:text="跳转到浏览器(显式)"
        android:onClick="skip2BrowserVisible"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">

    </Button>
    <Button
        android:layout_width="match_parent"
        android:text="跳转到浏览器(隐式)"
        android:onClick="skip2BrowserInvisible"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">

    </Button>
</LinearLayout>

 

Skip2BrowserActivity.java

package com.example.activitydemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class Skip2BrowserActivity extends Activity {


    private static final String TAG = "Skip2BrowserActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skip2browser);
    }

    /**
     * 这个方***在点击按钮的时候执行
     * @param view
     */

    public void skip2BrowserVisible(View view){

        Log.d(TAG,"skp2Browser");
        Intent intent =new Intent();
        /**
         * 組件的名称,也就是ComponentName =包名/类的路径名称,如包名一直,类的路径名称可以省略包名
         * 如:com.google.android.as/com.google.android.apps.miphone.aiai.app.AiAiPredictionService
         */
        //第一种写法
        // intent.setClassName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");

         //第二种写法
        ComponentName  componentName=new ComponentName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");
        intent.setComponent(componentName);

        startActivity(intent);
    }

    /**
     * 下面通过隐式意图来跳转到第三方应用
     * 1、创建Intent对象
     * 2、第二步给intent对象,设置Action、category,5.1以上要设置包名
     * 3、startActivity(intent)
     * @param view
     */
        public void skip2BrowserInvisible(View view) {
            Intent intent =new Intent();
            intent.setAction("android.intent.action.SEARCH");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setPackage("com.google.android.as");
            startActivity(intent);
        }

}                                                                                                                                                                                                                                                                                                 

 

全部评论

相关推荐

bg27强双非本,目前在学习golang后端gin框架部分,在b站找了一个轮子项目敲了一下,技术栈是gin&nbsp;+&nbsp;gorm&nbsp;+&nbsp;mysql&nbsp;+&nbsp;redis。我目前的想法是这一个月学习408和go八股以及刷算法然后在12月找个寒假实习然后大三下开始准备考研。我是考研意愿比较强烈,想问一下我是应该all&nbsp;in其中一个方向吗,我感觉我实习对我考研来说也是没什么帮助的好像。
牛客28967172...:毕业工作,考研,考公是完全不同的方向。 99%的人拼尽全力也只能把一个做好(能做好都已经是佼佼者了,比如进进大厂,考985或者考公) 如果你确定要考研可以不用学任何就业技术框架,也不用实习经验,刷题背知识点就行,但注意必须考92院校起步,因为这个年代双非硕毕业后完全不如双非本(互联网行业),可以说双非硕在互联网就业完全是负收益
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享
LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务