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 就为啥不行呢,,,

难道是权限的问题吗?

检查logcat之后,报错context空指针,那是哪门子的权限,,,

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

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

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值