package net.npaka.activityex;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
//アクティビティの呼び出し
public class ActivityEx extends Activity
implements View.OnClickListener {
private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
//アプリ起動時に呼ばれる
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//レイアウトの生成
LinearLayout layout=new LinearLayout(this);
layout.setBackgroundColor(Color.rgb(255,255,255));
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
//ボタンの生成
layout.addView(makeButton("Webページの表示:npaka.net","web"));
layout.addView(makeButton("地図の表示:Tokyo","map"));
layout.addView(makeButton("通話の開始 tel:117","call"));
layout.addView(makeButton("ダイアラーの表示","dial"));
layout.addView(makeButton("設定画面の表示","setup"));
layout.addView(makeButton("設定画面の表示","hello"));
}
//ボタンの生成
private Button makeButton(String text,String tag) {
Button button=new Button(this);
button.setText(text);
button.setTag(tag);
button.setOnClickListener(this);
button.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
return button;
}
//ボタンクリック時に呼ばれる
public void onClick(View v) {
String tag=(String)v.getTag();
try {
//Webページの表示
if (tag.equals("web")) {
Intent intent=new Intent("android.intent.action.VIEW",
Uri.parse("http://npaka.net"));
startActivity(intent);
}
//地図の表示
else if (tag.equals("map")) {
Intent intent=new Intent("android.intent.action.VIEW",
Uri.parse("geo:0,0?q=Tokyo"));
startActivity(intent);
}
//通話の開始
else if (tag.equals("call")) {
Intent intent=new Intent("android.intent.action.CALL",
Uri.parse("tel:117"));
startActivity(intent);
}
//ダイアラーの表示
else if (tag.equals("dial")) {
Intent intent=new Intent("android.intent.action.DIAL",
Uri.parse("tel:117"));
startActivity(intent);
}
//設定画面の表示
else if (tag.equals("setup")) {
Intent intent=new Intent("android.settings.SETTINGS");
startActivity(intent);
}
//HelloWorldの起動
else if (tag.equals("hello")) {
startActivity(this,"net.npaka.helloworld",
"net.npaka.helloworld.HelloWorld");
}
} catch (Exception e) {
toast(this,e.getMessage());
}
}
//クラス名指定によるアクティビティ起動
private static void startActivity(Activity activity,
String packageName,String className) throws Exception {
Intent intent=new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(packageName,className));
intent.removeCategory(Intent.CATEGORY_DEFAULT);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
//トーストの表示
public static void toast(Context context,String text) {
Toast.makeText(context,text,Toast.LENGTH_LONG).show();
}
} |