从一个app里面 跳转到另一个 app

1、功能介绍

从一个 app 里,点击跳转按钮,跳转到另一个app 里的 某一个界面
这里写图片描述

跳转的 app 界面还可以接受传输过来的数据
这里写图片描述

2、跳转的实现 MyJumpApp主要代码结构

这里写图片描述

2.1 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.menglux.myjumpaapp.MainActivity">

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转 app"
        android:textSize="30dp" />

</LinearLayout>
2.2 MainActivity.java 文件
package com.example.menglux.myjumpaapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "MainActivity: ";
    private String  appPackageName = "com.example.menglux.mytestjumpapp";
    private Button buttonId;

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

        initView();
    }

    //初始化组件
    private void initView() {
        buttonId = (Button) findViewById(R.id.button_id);
        buttonId.setOnClickListener(this);
    }


    public boolean isInstalledApp(Context myContext) {
        PackageManager myPackageMgr = myContext.getPackageManager();
        try {
            myPackageMgr.getPackageInfo(appPackageName, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            System.out.println(TAG + "没有安装 app: " + appPackageName);
            return (false);
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_id:

                boolean isInstallApp = isInstalledApp(this);
                if (isInstallApp) {

                    //跳转到app 指定的界面,并且传递参数
                    ComponentName componentName = new ComponentName(appPackageName,"com.example.menglux.mytestjumpapp.MainActivity");
                    Intent intent = new Intent();
                    intent.putExtra("test", "你好 ,MainActivity");
                    intent.setComponent(componentName);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    System.out.println(TAG + "跳转 app");


                    //根据包名来跳转  不能指定跳转界面
/*                    Intent intent = getPackageManager().getLaunchIntentForPackage(appPackageName);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("test", "你好 ,MainActivity");
                        startActivity(intent);*/

                } else {
                    goToMarket(this);
                }

                break;
                default:
                    break;
        }
    }


    public void goToMarket(Context myContext) {
        System.out.println(TAG  +  "没有安装 app  市场下载");
        Uri marketUri = Uri.parse("market://details?id=" + appPackageName);
        Intent myIntent = new Intent(Intent.ACTION_VIEW, marketUri);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myContext.startActivity(myIntent);
        return;
    }
}

3 、被跳转的app 一些设置 myTestJumpApp

3.1、代码结构

这里写图片描述

在 AnroidManifest 文件中给 activity 节点设置 Android:exported=”true”,该属性表示当前 activity 能否被另外一个Application 的组件启动,true允许启动,false不允许。默认是false。注意:该属性是四大组件都拥有的。

3.2、 AndroidManifest.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.menglux.mytestjumpapp">

    <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"
            android:exported="true"> <!--该属性表示当前 activity 能否被另外一个Application 的组件启动,true允许启动,false不允许-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
3.2、 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.menglux.mytestjumpapp.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView_id"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="30dp" />

</LinearLayout>
3.3、ManiActivity.java 文件
package com.example.menglux.mytestjumpapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private String TAG = "MainActivity: ";
    private TextView textViewId;

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


        initView();

        getIntentData();
    }


    //获取传入的数据
    private void getIntentData() {
        Intent intent = getIntent();
        String  str = intent.getStringExtra("test");
        textViewId.setText(str);
        System.out.println(TAG + "传入的数据是: " + str);
    }


    //初始化组件
    private void initView() {
        textViewId = (TextView) findViewById(R.id.textView_id);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/82148753