Android プログラミング アクティビティ
アクティビティの起動
@Override public void onClick(View v) { String tag = (String)v.getTag(); try{ if(TAG_WEB.equals(tag)){ Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://python.org")); startActivity(intent); }else if(TAG_MAP.equals(tag)){ Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("geo:0,0?q=Toyko")); startActivity(intent); }else if(TAG_CALl.equals(tag)){ Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:117")); startActivity(intent); }else if(TAG_DIAL.equals(tag)){ Intent intent = new Intent("android.intent.action.DIAL", Uri.parse("tel:117")); startActivity(intent); }else if(TAG_SETUP.equals(tag)){ Intent intent = new Intent("android.settings.SETTINGS"); startActivity(intent); } }catch (Exception e){ } } // 自作アクティビティの起動 private static void startActivity(Activity activity, String packageName, String className){ 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); } private void toast(String text){ if(text == null) text = ""; Toast.makeText(this, text, Toast.LENGTH_LONG).show(); }
パラメータ渡し
送る側アクティビティ
@Override public void onClick(View v) { // アプリ内のアクティビティの呼び出すためのインテント生成 Intent intent = new Intent(this,MyActivity.class); // パラメータ設定 intent.putExtra("text", textView.getText().toString()); // アクティビティの呼び出し startActivityForResult(intent,REQUEST_TEXT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_TEXT || requestCode == RESULT_OK){ // インテントからのパラメータ取得 String text = ""; Bundle extras = data.getExtras(); if(extras != null) text = extras.getString("text"); textView.setText(text); } }
受け取る側アクティビティ
public class MyActivity extends Activity implements View.OnClickListener{ private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final static int MP = LinearLayout.LayoutParams.MATCH_PARENT; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 戻り値の指定 setResult(Activity.RESULT_CANCELED); // インテントからのパラメータ取得 String text = ""; Bundle extras = getIntent().getExtras(); if(extras != null) text = extras.getString("text"); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); Button button = new Button(this); button.setText("OK"); button.setLayoutParams(new LinearLayout.LayoutParams(WC, WC)); button.setOnClickListener(this); layout.addView(button); editText = new EditText(this); editText.setText(text); editText.setLayoutParams(new LinearLayout.LayoutParams(MP,WC)); layout.addView(editText); } @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("text",editText.getText().toString()); setResult(Activity.RESULT_OK,intent); // アクティビティの終了 finish(); } }
ブロードキャスト
送る側
@Override public void onClick(View v) { // インテントのブロードキャスト Intent intent = new Intent(); intent.setAction("com.example.yuto.broadcastreceiveex.VIEW"); intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.putExtra("TEXT", "ブロードキャストレシーバのテスト"); sendBroadcast(intent); }
レシーバ
public class TextReceiver extends BroadcastReceiver { // インテントの受信 @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String text = bundle.getString("TEXT"); toast(context,text); } private static void toast(Context context, String text){ Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG); toast.show(); } }
Manifest.xmlに追加したもの
<receiver android:name=".TextReceiver" android:exported="false" > <intent-filter> <action android:name="com.example.user.broadcastreceiveex.VIEW" /> </intent-filter> </receiver>
参考文献
Androidプログラミングバイブル SDK5/4対応 (smart phone programming bible)
- 作者: 布留川英一
- 出版社/メーカー: ソシム
- 発売日: 2015/01/26
- メディア: 単行本
- この商品を含むブログ (2件) を見る