zoukankan      html  css  js  c++  java
  • Bundle、Intent、SharedPreferences

    Intent与Bundle的共同点:都继承Parcelable

    Intent传值与Bundle传值的区别

    eg:我现在要从A界面   跳转到B界面或者C界面   
    这样的话 我就需要写2个Intent  如果你还要涉及的传值的话 你的Intent就要写两遍添加值的方法 那么 如果我用1个Bundle  直接把值先存里边 然后再存到Intent中 就更简洁吗?

    eg:  如果我现在有  Activity A ,B ,C;
    现在我要把值通过A经过B传给C
    你怎么传 如果用Intent的话   A-B先写一遍   再在B中都取出来 然后在把值塞到Intent中 再跳到C   累吗?
    如果我在A中用了 Bundle 的话  我把Bundle传给B 在B中再转传到C  C就可以直接去了 
    这样的话 还有一个好处 就是在B中 还可以给Bundle对象添加新的 key - value  同样可以在C中取出来

         Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。在Intent的使用中你看不到直接的函数调用,相对函数调用来说,Intent是更为抽象的概念,利用Intent所实现的软件复用的粒度是Activity/Service,比函数复用更高一些,另外耦合也更为松散。

    理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似,只是复用的粒度有所差别;另一种是隐式的Intent,即Intent的发送者在构造Intent对象时,并不知道也不关心接收者是谁,这种方式与函数调用差别比较大,有利于降低发送者和接收者之间的耦合。另外Intent除了发送外,还可用于广播,这些都将在后文进行详细讲述。

    Intent和Bundle实现从一个Activity带参数转换到另一个Activity的代码例子

     1 if(et_username.getText().toString().equals("peidw") && et_password.getText().toString().equals("123456") ){
     2              Intent = new Intent(); 
     3                    Bundle bundle = new Bundle();  
     4                    bundle.putString("USERNAME", et_username.getText().toString());  
     5                    intent.putExtras(bundle);
     6                    intent.setClass(loginactive.this, informationactive.class); 
     7                    startActivity(intent);
     8 }else{
     9                    intent = new Intent();  
    10                    intent.setClass(loginactive.this, errorpageactive.class);  
    12                    startActivity(intent);  
    13 }
     1 protected void onCreate(Bundle savedInstanceState) { 
     2 super.onCreate(savedInstanceState);
     3 this.setContentView(R.layout.informationactive);
     4 tv = (TextView)findViewById(R.id.first_page_info);
     5 Bundle bundle = this.getIntent().getExtras();  
     6 String str=bundle.getString("USERNAME");
     7 tv.setText(str);
     8 button_back = (Button)findViewById(R.id.back);  
     9 button_back.setOnClickListener(new OnClickListener() {  
    10          public void onClick(View view) { 
    11                 Intent intent = new Intent(); 
    12                 intent.setClass(informationactive.this,mainactive.class); 
    13                 startActivity(intent);  
    14          }
    15    });

     SharedPreferences是一种轻量级的数据存储机制,它将一些简单数据类型的数据,包括boolean类型、int类型、float类型、long类型以及String类型的数据,以键值对的形式存储在应用程序的私有Preferences目录(/data/data/<包名>/shared_prefs/中。这种Preferences机制广泛应用于存储应用 程序中的配置信息。

    在Adndroid平台上,只需要用一个Context的对象调用getSharedPreferences(String name,int mode)方法传入Preferences文件名和打开模式,就可以获得一个SharedPreferences的对象。若该Preferences文件不存在,在提交数据后会创建该Preferences文件。利用SharedPreferences对象可以调用一些getter方法,传入相应的键来读取数据。要对Preferences文件的数据进行修改,首先利用SharedPreferences对象调用edit方法获得一个内部类Editor的对象,然后用这个Editor对象就可以对Preferences文件进行编辑了。注意,编辑完毕后一定要调用commit()方法,这样才会把所做的修改提交到Preferences文件当中去。下面是一个利用Preferences机制来保存Editor中所输入的字符串的示例。

     1 package com.action;  
     2   
     3 import android.app.Activity;  
     4 import android.content.SharedPreferences;  
     5 import android.os.Bundle;  
     6 import android.widget.EditText;  
     7   
     8 public class Tab extends Activity {  
     9     EditText edit;  
    10     SharedPreferences sp;  
    11     final static String NAME = "my_sp";  
    12     final static String EDIT_KEY = "edit";  
    13       
    14     /** Called when the activity is first created. */  
    15     @Override  
    16     public void onCreate(Bundle savedInstanceState) {  
    17         super.onCreate(savedInstanceState);  
    18         setContentView(R.layout.main);  
    19           
    20         edit = (EditText) findViewById(R.id.edit);  
    21         sp = getSharedPreferences(NAME, 0);  
    22         String str = sp.getString(EDIT_KEY, null);  
    23         if(str != null){  
    24             edit.setText(str);  
    25         }  
    26     }  
    27     @Override  
    28     protected void onDestroy() {  
    29         super.onDestroy();  
    30         SharedPreferences.Editor editor = sp.edit();  
    31         editor.putString(EDIT_KEY, String.valueOf(edit.getText()));  
    32         editor.commit();  
    33           
    34     }  
    35 }  

    SharedPreference数据以XML格式存放的。我们可以在 File Explorer中找到,目录为:data/data/包路径/shared_prefs

  • 相关阅读:
    实现分布式爬虫
    hadoop安装文档
    远程连接mysql和redis配置
    scrapy(2)
    10月10号动手动脑
    10月6日和动手动脑
    10月4日
    课程总结
    又学java的第一天
    第一节测试总结
  • 原文地址:https://www.cnblogs.com/cuijl/p/4705732.html
Copyright © 2011-2022 走看看