ANDROIDノート:Activityの明示的および暗黙的呼び出し
7036 ワード
package com.example.android_activity.test;
import com.example.android_activity.R;
import android.app.LauncherActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MyTestActivity extends LauncherActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_1,
new String[] { "com1", "com2" });
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected Intent intentForPosition(int position) {
// TODO Auto-generated method stub
Intent intent = null;
switch (position) {
case 0:
// 1
intent = new Intent(MyTestActivity.this, CompActivity.class);
// 2
ComponentName componentName = new ComponentName(
MyTestActivity.this, CompActivity.class);
intent.setComponent(componentName);
// 3
ComponentName componentName2 = new ComponentName(
"com.example.android_activity.test.MyTestActivity",
"com.example.android_activity.test.CompActivity");
intent.setComponent(componentName);
break;
case 1:
intent = new Intent();
//
intent.setAction("com.ex.one");
intent.addCategory("android.intent.category.DEFAULT");
break;
}
return intent;
}
}
暗黙的に呼び出すにはAndroidManifestが必要です.xmlでactivityのactionとcategoryを構成し、
この方式は、他のプログラムにプログラムのactivityを呼び出すことができる.
<activity
android:name="com.example.android_activity.test.CompActivity"
android:label="@string/app_name"
android:exported="true"
>
<!-- android:exported="true" -->
<intent-filter>
<action android:name="com.ex.one" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>