隱式調用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。