🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
隐式调用Activity和BroadcastReceiver调用方法之前已经介绍过了。今天只是来做下4个实验,假设B通过Intent隐式调用A,如果A没有一个Activity有 ~~~ <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> ~~~ 这个设置会怎么样? 下面是Activity的实验: ~~~ package com.example.intenta; import android.app.Activity; import android.os.Bundle; public class ShowType extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.show_type); } } ~~~ 上面是A的JAVA代码,下面是A的XML代码,只是定义了一个TextView ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:text="启动A" /> </LinearLayout> ~~~ ~~~ package com.example.intenta; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 上面是A的另外一个Activity的代码(A有两个Activity,通过控制在这一个Acticity在manifest是否设置android.intent.action.MAIN等条目来控制实验变量) ~~~ <activity android:name="com.example.intenta.ShowType" > <intent-filter> <action android:name="com.mytest.IntentA" > </action> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.mytest.startA" /> </intent-filter> </activity> ~~~ 这是A的IntentFilter ~~~ package com.example.intentb; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.mytest.IntentA"); intent.addCategory("com.mytest.startA"); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 这里是B的JAVA代码,通过点击按钮来隐式调用A Activity实验结果: 1)在有设置的情况下:能够正常通过B启动A 2)没有设置的情况下:能够正常通过B启动A 实验结果分析: Activity的Intent启动过程不受是否设置 ~~~ <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> ~~~ 下面是BroadcastReceiver的实验: 代码和上面基本类似,下面只放出A中BroadcastReceiver的代码: ~~~ package com.example.intenta; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class ShowType extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "启动A...", Toast.LENGTH_LONG).show(); } // @Override // protected void onCreate(Bundle savedInstanceState) { // // TODO Auto-generated method stub // super.onCreate(savedInstanceState); // setContentView(R.layout.show_type); // } } ~~~ 实验结果: 1)在有设置的情况下:B能够正常通过Intent启动A的BroadcastReceiver 2)在没有设置的情况下:失败 最后实验结果: ~~~ <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> ~~~ 如果A中没有一个Activity有这段IntentFilter设置,那么通过Intent,B能够启动A的Activity但是不能启动A的BroadcastReceiver。