在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 手機的方法,,,我再來展示
以上就是 所有程序,,,,


轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/295688.html
標籤:其他
上一篇:vue頁面跳轉取消上一個頁面請求
