关闭

Intent的用法

标签: Intent
231人阅读 评论(0) 收藏 举报
分类:
Intent的用法:

    java.lang.Object 
    android.content.Intent 


    public class Intent
    extends Object
    implements Parcelable, Cloneable


An intent is an abstract description of an operation to 
be performed. It can be used with startActivity to launch
 an Activity,broadcastIntent to send it to any interested 
 BroadcastReceiver components, 
 and Context.startService(android.content.Intent) or
  Context.bindService(android.content.Intent, 
 android.content.ServiceConnection, int) to 
 communicate with a background Service. 



 Intent 作用:意图,传输,要做的事

 构造方法:

 1 public Intent()
 Create an empty intent.

 2 public Intent(String action)

 Create an intent with a given action.
 action - The Intent action, such as ACTION_VIEW.

 for example the system VIEW action is android.intent.action.VIEW; 
 an application's custom action would be something like 
 com.google.app.myapp.CUSTOM_ACTION.

 3 public Intent(String action, Uri uri)

 action - The Intent action, such as ACTION_VIEW.
 uri - The Intent data URI.

  例如:
Uri uri = Uri.parse(“http://www.google.com”);  
Intent it = new Intent(Intent.ACTION_VIEW,uri);  
startActivity(it);


 发送Email

Uri uri = Uri.parse(“mailto:xxx@abc.com”);  
Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
startActivity(it);  

Intent it = new Intent(Intent.ACTION_SEND);  
it.putExtra(Intent.EXTRA_EMAIL, “me@abc.com”);  
it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  
it.setType(“text/plain”);  
startActivity(Intent.createChooser(it, “Choose Email Client”));  

Intent it=new Intent(Intent.ACTION_SEND);  
 String[] tos={“me@abc.com”};  
 String[] ccs={“you@abc.com”};  
it.putExtra(Intent.EXTRA_EMAIL, tos);  
it.putExtra(Intent.EXTRA_CC, ccs);  
it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  
it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);  
it.setType(“message/rfc822″);  
startActivity(Intent.createChooser(it, “Choose Email Client”));  



 4 public Intent(Context packageContext,
      Class<?> cls)

 例如 
 Intent intent = new Intent(MainActivity.this,SecondActivity.class);
 startActivtiy(intent);

packageContext - A Context of the application package implementing this class.
cls - The component class that is to be used for the intent.

5 public Intent(String action,
      Uri uri,
      Context packageContext,
      Class<?> cls)

综合了上面1234


常用的方法:
parseUri
public static Intent parseUri(String uri,
              int flags)
                       throws URISyntaxException

Create an intent from a URI
uri - The URI to turn into an Intent.
flags - Additional processing flags. Either 0 or URI_INTENT_SCHEME. 


setDataAndType
public Intent setDataAndType(Uri data,
                    String type)
data - The Uri of the data this intent is now targeting.
type - The MIME type of the data being handled by this intent. 

例如:
Uri uri = Uri.parse(“file:///sdcard/song.mp3″);  
Intent it = new Intent(Intent.ACTION_VIEW, uri);  

it.setDataAndType(uri, “audio/mp3″);  
context.startActivity(it);  


putExtra
public Intent putExtra(String name,
              CharSequence value)

name - The name of the extra data, with package prefix.//包名加前缀Intent.EXTRA_TEXT
value - The CharSequence data value. 

例如:it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  

或者其他 的也行


Intent传递对象:
因为Intent传递的类型有限,所以下面的两种方法可以实现自定义传递一个对象

第一种是对象实现Serializable:

public class Person implements Serializable{
    .....
}

Person person = new Person("Tom",20);

Intent intent = new Intent(F.this,S.class);
intent.putExtra("person_data",person);
startActivity(intent);

第二种对象实现Parcelable
public class Person implements Parcelable{
    .....

    @Override
    public int describeCotents(){
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest,int flags){
        dest.writeString(name);//写出Name
        dest.writeInt(age);
    }


public static final Parcelable.Creator<Person> CREATOR =

new Parcelable.Creator<Person>(){
    @Override
    public Person createFromParcel(Parcel source){
        Person person = new Person();
        person.name = source.readString();//读取name,注意和上面的顺序要一致
        person.age = source.readInt();

        return person;
    }

    @Override
    public Person[] new Array(int size){
        return new Person[size];
    }
};
}

用法和上面的Serializable一样

读取一个Person对象:
Person person = (Person)getIntent().getParcelableEXtra("person_data");

Person person = (Person)getIntent().getSerializaleEXtra("person_data");

以后再补充 @_@

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17737次
    • 积分:389
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论
  • 图片加载器

    xiaoxxxlong: 能把 demo 地址给出来么 直接跑着测试哇。。最近开发需要处理图片的东东 ,,谢谢楼主