IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Studio 自己app启动另一个app 启动别的应用 启动自己的另一个app 启动自己的另一个应用 启动其他应用 解决方法 -> 正文阅读

[移动开发]Android Studio 自己app启动另一个app 启动别的应用 启动自己的另一个app 启动自己的另一个应用 启动其他应用 解决方法

在App中不免会遇到自己做的app需要启动另一个的,其实只要用Intent就可以解决,但是本人今天从中文到下午一直就头皮发麻,一直没有任何作用,真滴是🐕(gou)?(ri)🚗(di)!!!

这次学习是通过包名来启动,很简单吧,但是我却就很小白了,,,,我差点就没有把屏幕按穿。

包名的话其实非常好找到了,就比如说在MainActivity.java中就能看见

package com.qiujie.template02; //这个就是包名

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

我其实也用过其他的方法,好像是啥隐式启动啥的,好像是通过AndroidManifest.xml中的

intent-filter,我称其为,应用过滤器,,,,(我虽然可以上网搜,但是我就不,哎~我有手,就不做,哎~就是玩)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qiujie.myapplication">
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"

        android:label="测试"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity android:name=".MainActivity3" android:label="第二界面" android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"></activity>
        <activity android:name=".MainActivity2" android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"/>
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
android:launchMode="singleTask"
            >
            <intent-filter>          //就是在这里的代码,但是这样的代码还不能找到咯。。。
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

现在,展示跳转app的源代码。

PackageManager p = MainActivity.this.getPackageManager();

                Intent intent2=new Intent();

                intent2 =  p.getLaunchIntentForPackage("这里添加需要开启的包名!!!");
                if(intent2==null)
                {
                    Toast.makeText(MainActivity.this,"没有此应用,或请重新下载再试吧",Toast.LENGTH_SHORT).show();
                }
                else {
                    startActivity(intent2);
                }

第一步,创建当前布局的packageManager;

第二步,创建Intent实例;

第三步判断,这里需要有判断,是否为空,我通过 看其他文章以及自己的亲自实验,得知,如果是空指针,会闪退,是否是空指针我不知道,但是,闪退绝对是有的,绝对。。。

第四步,使用startActivity开启Intent。

这样就可以跳转了。

但是,这样我根本就在我手机没有用,,,,

以下是我的源代码,超级简单,两个SW,两个ET,但是就只有一个按钮有用,其他的都没有码代码呢。。

package com.qiujie.template02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private EditText et_number,et_sms;
private Button btn_call,btn_send;
    private View.OnClickListener onclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_call)
            {
//                Toast.makeText(MainActivity.this,"点击打电话",Toast.LENGTH_SHORT).show();

               // String action = "android.intent.action.   MAIN";
           //     Intent intent = new Intent(action);
//                String number = et_number.getText().toString();
                //intent.setData(Uri.parse("tel:"+number));

                PackageManager p = MainActivity.this.getPackageManager();

                Intent intent2=new Intent();

                intent2 =  p.getLaunchIntentForPackage("com.qiujie.myapplication");
//                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if(intent2==null)
                {
                    Toast.makeText(MainActivity.this,"没有此应用,或请重新下载再试吧",Toast.LENGTH_SHORT).show();
                }
                else {
                    startActivity(intent2);
                }


            }
            else if(v == btn_send)
            {
                Toast.makeText(MainActivity.this,"点击发短信",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package"+getPackageName()));
                if(intent == null)
                {
                    Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"失败1",Toast.LENGTH_SHORT).show();
                    startActivity(intent);

                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = findViewById(R.id.et_number);
        et_sms = findViewById(R.id.et_sms);
        btn_call = findViewById(R.id.btn_call);
        btn_send = findViewById(R.id.btn_send);

        btn_call.setOnClickListener(onclickListener);
        btn_send.setOnClickListener(onclickListener);

        View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (v == btn_call)
                {
                    Toast.makeText(MainActivity.this,"长按打电话",Toast.LENGTH_SHORT).show();
                }
                else if(v == btn_send)
                {
                    Toast.makeText(MainActivity.this,"长按发短信",Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        };
        btn_call.setOnLongClickListener(onLongClickListener);
        btn_send.setOnLongClickListener(onLongClickListener);

    }
}

以下是我的布局文件

<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="59dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"

            android:layout_height="match_parent"
            android:gravity="center"
            android:text="电话号码:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_number"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="请输入电话号码" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="59dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"

            android:layout_height="match_parent"
            android:gravity="center"
            android:text="短信内容:"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/et_sms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="请输入短信内容" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="98dp"
        android:orientation="horizontal">

        <Button
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/btn_call"
            android:layout_width="123dp"
            android:layout_height="wrap_content"
            android:text="打电话" />

        <Button
            android:layout_marginTop="10dp"

            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/btn_send"
            android:layout_width="123dp"
            android:layout_height="wrap_content"
            android:text="发短信" />
    </LinearLayout>
</LinearLayout>

图新界面如下

我所要开启的app就不展示了咯,

但是我用我实体手机却一丢丢用没有,一直打不开,一直显示Toast为空,很烦,,,

到将近9点中,我用我对象的华为手机无助的把我两个app用adb连接(PS:为啥华为 mate 30 pro开发者选项中,没有Wlan? adb的开关呢,iqoo7 就有,好评,,,虽然我照样可以wlan 来连接就是,,,端口5555吧,应该。没有试过),安装好,哈拉少呀!!能用,还贼快,🐕(gou)?(ri)🚗(di)!!!这是为啥,我的iqoo 7 就为啥不行呢,,,

结果就是,能用,但是也不能用。。。如果能找到VIvo 手机的方法,,,我再来展示

以上就是 所有过程。。。。

?

?

?

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-23 16:48:38  更:2021-08-23 16:50:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/28 6:41:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码