Activityの7つの属性

32634 ワード

Activityの7つのプロパティには、次のものがあります.
     ComponentName   Extra   Flags  Category   Action    Data   Type
1)ComponentName:ComponentNameプロパティを指定したIntentは、どのコンポーネントを起動するかが明確になっているため、このIntentは明示的なIntentと呼ばれ、ComponentNameプロパティを指定していないIntentは暗黙的なIntentと呼ばれています.暗黙的なIntentはどのコンポーネントを起動するかを明確にしていません.アプリケーションはIntentが指定したルールに基づいて条件に合致するコンポーネントを起動します.
Componentnameは、他のページへのジャンプを実装したり、他のアプリケーションコードへのジャンプを実装したりする例を以下に示します.
       xmlCode:
"1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/btn_01"
        android:onClick="btnClick"
        android:text=" "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btnClick"
        android:layout_below="@id/btn_01"
        android:text=" "/>
RelativeLayout>
           javaCode:
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
    }
    public void btnClick(View view){
        switch (view.getId()){

            case R.id.btn_01:
                // 1.componentname        
                Intent intent  = new Intent();
                ComponentName componentName = new ComponentName(MainActivity.this,ThridActivity.class);
                intent.setComponent(componentName);
                startActivity(intent);
                break;
            case R.id.btn_02:
                // 2.componentname  
                Intent intent1 = new Intent();
                //1 . 
                //2 . Activity 
                ComponentName componentName1 = new ComponentName("zhanghao.androidday06_activityforbitmap",
                        "zhanghao.androidday06_activityforbitmap.MainActivity");
                // 
                intent1.setComponent(componentName1);
                // 
                startActivity(intent1);
                break;
        }
    }
}
 

2)Flags:Flagを設定ことで、LaunchModeと基本的に同じActivityの起動モードを設定することができます.
            javaCode:
        
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends BaseActivity {

   public final static String TAG ="MainActivity";
   private TextView textView;
 
   @SuppressLint("SimpleDateFormat")
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.addActivityIntance(this);
        textView = (TextView)findViewById(R.id.text);
    }  
    public  void setClick(View v ){     
       Intent  intent  = new  Intent(MainActivity.this,SecondActivity.class);
       intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
       //setFlags 
        //1. , !
       //2. flags  Flag_activity_single_top    -->singleTop
       //3.Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP
       // -->singleTask
       //  flags  singleIntance 
       startActivity(intent);
    }
}

3)Extras:Activity間でデータを転送するために使用され、Extraは基本タイプ、Stringタイプ、SerializableまたはParcelableインタフェースを実現したクラスを転送することができる
           javaCode:
          
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   public void textClick(View view){   
      Intent intent = new Intent();
      intent.setClass(MainActivity.this, SecondActivity.class);
      // extra    
       intent.putExtra("send", " ! !!");
      intent.putExtra("it", 94250);
      intent.putExtra("qq", true);
      // 
      startActivity(intent2);  
   }
   
}
SecondActivity受信した値:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends Activity{
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
       setContentView(R.layout.secondlayout);
       Intent intent = getIntent(); // 
       if (intent != null) {
          //Intent    Key    
          String sendString = intent.getStringExtra("send");
          //defaultValue     key          int it = intent.getIntExtra("it", 250);
          // boolean   
          boolean qq =intent.getBooleanExtra("qq", false);
         Toast.makeText(this, sendString+it+qq, Toast.LENGTH_LONG).show();         
      }
   }
}

ActionCategory Type Dataは基本的に一緒に使います.
4)Action:intentのActionブロードキャストを使用する場合に多く使用されます.Activityでは、Actionを設定することで、ThirdActivityなどのActivityを暗黙的に起動することができます.リストファイルでは、次のように構成されています.
<activity android:name=".ThirdActivity" >

    <intent-filter>
        <action android:name="custom1" />
        <action android:name="custom2" />

        <category android:name="category1" />
        <category android:name="android.intent.category.DEFAULT" />

        <data
            android:mimeType="text/*"
            android:scheme="http" />
    intent-filter>
activity>
インベントリファイルでこのような構成をすると、ThirdActivityはこの動作に応答します.
5)Category:このような暗黙的な起動方法を使用してActivityを起動するときは、actionとcategoryが一致しなければ、Activityが正常に起動しません.categoryを定義していない場合は、システムのデフォルトのcategoryを一時的に使用することができます.つまり、categoryは存在しないことはできません.
6)Data:Data属性は、通常、アクション属性に操作を提供するためのデータである.Data属性の値はUriオブジェクトです.dataを設定することで、電話、メール、Webページの開発などの操作を行うことができます.
7)Type:Type属性は、Dataで指定されたUriに対応するMIMEタイプを指定するために使用されます.MIMEは「abc/xyz」のような文字列フォーマットに該当すればよい.
         javaCode:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    // SecondActivity
    // : action category data
    public  void btnClick(View view){
        Intent  intent = new Intent();
        //intent Action,    Action
        intent.setAction("custom1");
        //category 
         intent.addCategory("category1");
        intent.addCategory("android.intent.category.DEFAULT");
        // Data   Type   , setDataAndtype 
        intent.setDataAndType(Uri.parse("http://www.baidu.com"),"text/plain");
        startActivity(intent);
    }
}