隐式调用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); } }
<?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; } }
<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>
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; } }
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>