安卓之intent的显式、隐式、过滤器的使用!
布局非常简单,被启动activity就一个textview,启动activity就一个button!有需要的可以下载,我把整个软件打包上传了我的资源
整个工程目录图:
整个软件的AndroidMainfest.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helios.learnintent"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity" android:label="Main2Activity"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="com.example.helios.learnintent.intent.action.Main2Activity" /> </intent-filter> </activity> <activity android:name=".Main3Activity"> <intent-filter> <category android:name="android.intent.category.DEFAULT"/> <action android:name="com.example.helios.learnintent.intent.action.Main2Activity"/>
//过滤器的使用 <data android:scheme="app"/> </intent-filter> </activity> </application> </manifest>
显式Intent的使用:
package com.example.helios.learnintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //显示Intent跳转 //Intent intent = new Intent(MainActivity.this,Main2Activity.class);
} }); } }
隐式Intent的使用:
启动Activity
package com.example.helios.learnintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Intent intent = new Intent("com.example.helios.learnintent.intent.action.Main2Activity"); //隐式intent跳转,我们在写action的时候,一般情况下都在被启动的activity里面添加一个静态常量,在启动方式里面可以直接写,如下: Intent intent = new Intent(Main2Activity.ACTION); startActivity(intent); } }); } }
被启动Activity
package com.example.helios.learnintent; import android.app.Activity; import android.os.Bundle; /** * Created by helios on 2018/6/1. */ public class Main2Activity extends Activity { public static final String ACTION = "com.example.helios.learnintent.intent.action.Main2Activity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } }
Intentg隐式加上过滤器的使用,
package com.example.app1; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过隐式Intent来跨应用启动,如果你不想你的应用被启动,你可以在你的被启动应用里面设置android:exported="false" //intent过滤器在要指定启动的activity里面设置 <data android:scheme="app"/>,然后在另一个app里面加上Uri.parse("app://hello") findViewById(R.id.btn_start_main2activity).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { startActivity(new Intent("com.example.helios.learnintent.intent.action.Main2Activity", Uri.parse("app://hello"))); }catch (Exception e){ Toast.makeText(MainActivity.this, "无法启动被指定应用!", Toast.LENGTH_SHORT).show(); } } }); } }