day4.27總結_Intent和Application

來源:互聯網
上載者:User

標籤:

一、Intent 對象 (1)Intent是什麼

1)意圖,信使

2)值對象(封裝資料,實現資料傳遞)

(2)Intent對象的應用場合

1)啟動組件(activity,service,BroadcastReceiver)

2)停止service,解除receiver的動態註冊

3)資料傳遞(組件之間)

(3)Intent對象實現原理及過程

1)封裝意圖資訊(你要做什麼)

2)封裝資料資訊(實現資料傳遞)

startActivity(intent)

startService(intent)

sendBroadcastReceiver(intent)

 

意圖會通過context對象的相關傳遞給底層系統,底層根據intent中封裝的具體意圖資訊找到對應的組件,並啟動他。

(4)意圖對象應用方式

1)顯式意圖(明確指定要啟動的組件)

2)隱式意圖(沒有明確指定要啟動的組件,只是傳遞字串給底層系統,底層去匹配對象)

 

對於一個隱式意圖對象常用的配置:(在intent-filter中實現)

1)action(代表具體意圖)

2)Category (代表分類,環境)

3)data (資料,可以指定類型)

說明:在使用隱式意圖啟動activity時,此activity的intent-filter中需要添加一個預設分類(DEFAULT)

 

隱式意圖一般應用於跨app啟動其它組件.

擴充:PendingIntent(延遲意圖),此對象內部可以封裝一個意圖對象。

 

Intent對象其它配置:

1)setFlags(int n):重點關注的是和啟動模式相關一些標記

2)setDataAndType(uri,type)

3)......

例子1:顯式意圖與隱式意圖

public class MainActivity extends Activity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

 

//顯式意圖

public void onClick01(View v){

 //startActivity(new Intent(this,OtherActivity01.class));

 Intent intent=new Intent();

 intent.setClass(this, OtherActivity01.class);  直接類名

 //intent.setComponent(new ComponentName(this, OtherActivity01.class));

 

 //startActivity(intent);

 Bundle options=//封裝動畫資訊

 ActivityOptions.makeCustomAnimation(this,

 android.R.anim.slide_in_left,//可以自己定製

 android.R.anim.slide_out_right).toBundle();

 startActivity(intent,options);

 //startActivities(intents)

}

public void onClick02(View v){

  //對於此intent對象在啟動activity,底層會自動添加一個分類

  Intent intent=new Intent("action.other");   加入字串

  intent.addCategory("category.other");

  intent.setType("text/*");

  startActivity(intent);

}

}

例子2:Intent傳遞對象和啟動相機

class Student implements Serializable{  直接實現介面

private static final long serialVersionUID = -1811479799107069425L;

String name;

    public Student(String name) {

     this.name=name;

}

    @Override

    public String toString() {

     return name;

    }

}

class Elephant implements Parcelable{  實現Parcelable後,重寫方法

private String name;

private int age;

public Elephant(String name,int age) {

this.name=name;

this.age=age;

}

//還原序列化

public Elephant(Parcel source){

this.name=source.readString();

    this.age=source.readInt();

}

@Override

public String toString() {

return name+"/"+age;

}

@Override

public int describeContents() {

// TODO Auto-generated method stub

return 0;

}

/**序列化*/

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(name);

dest.writeInt(age);

}

//藉助此對象實現還原序列化操作

public static final Creator<Elephant> CREATOR=

new Creator<Elephant>() {

//還原序列化

@Override

public Elephant createFromParcel(Parcel source) {

return new Elephant(source);

}

@Override

public Elephant[] newArray(int size) {

// TODO Auto-generated method stub

return new Elephant[size];

}

};

}

 

public class MainActivity extends Activity {

 

private ImageView imageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

    imageView=(ImageView) findViewById(R.id.imageView1);

}

public void onClick01(View v){

Intent intent=new Intent(this,OtherActivity01.class);

    intent.putExtra("flag01", true);

    Bundle b=new Bundle();//key/value

    b.putBoolean("flag02",false);

    b.putString("flag03", "helloworld");

    intent.putExtra("data", b);

    //可以封裝序列化對象

    intent.putExtra("student",new Student("陳靜"));

    intent.putExtra("elephant",new Elephant("大象",100));

    startActivity(intent);

}

private Button btn;

public void onClick02(View v){

btn=(Button)v;

startActivityForResult(

new Intent(this,OtherActivity02.class),100);//100表示請求碼(標識這是哪個請求)

}

//通過startActivityForResult方法啟動的activity

//在關閉時會執行此方法

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   Log.i("TAG", "resultCode="+resultCode);

   if(requestCode==100&&resultCode==200){

   String item=data.getStringExtra("itemKey");

   btn.setText(item);

   }else if(requestCode==101){

   Bundle bundle=data.getExtras();

   //bundle.keySet();

   Bitmap bitMap=(Bitmap)bundle.get("data");

   imageView.setImageBitmap(bitMap);

   }

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if(item.getItemId()==R.id.cap){

//啟動相機程式

startActivityForResult(

new Intent(MediaStore.ACTION_IMAGE_CAPTURE),

101);

}

return super.onOptionsItemSelected(item);

}

}

public class OtherActivity02 extends ListActivity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.activity_other_activity02);

    setListAdapter(new ArrayAdapter<String>(

    this, android.R.layout.simple_list_item_1,

    new String[]{"A","B","C"}));

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

    String item=(String) l.getItemAtPosition(position);

    Intent intent=new Intent();

    intent.putExtra("itemKey", item);

    setResult(200,//響應碼

    intent);//設定響應資料

    finish();

}

}

例子3:預設啟用系ImageView彈出圖片

public void onClick01(View v){

Intent intent=new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(

Uri.fromFile(

    new File("/mnt/sdcard/png_01.png")),"image/*");

startActivity(intent);

}

 

二、Application 對象 (1)Application是什麼

1)Android 中的一個Context對象(資源訪問能力)

2)Android 中的全域訪問對象(生命週期同APP生命週期相同)

(2)Application對象應用場合

1)為應用組件提供全域的資料訪問(共用資料)

2)記錄相應資料資訊(不適合大量資料)

 

例如:

1)使用者登入成功以後的使用者資訊。

2)各個組件都要使用的少量資料資訊。

(3)Application對象的應用

1)編寫(繼承Application),

2)註冊(清單設定檔中指定application的name屬性)

3)生命週期(onCreate,......)

 

說明:我在Application中儲存資料也可以直接藉助靜態屬性儲存區資料。

 

public class MyApplication extends Application {

public static User user;

}

<application

        android:name="com.tarena.elts.MyApplication"

 

day4.27總結_Intent和Application

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.