有关Intent的android的API

来源:互联网 发布:mac能下sai吗 编辑:程序博客网 时间:2024/06/11 04:29
1,Intent(意图),这在android中十分常用,常用于激活系统组件(如不同的Activity,broadcast和Service)和在系统组件之间传递数据
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
分为
显式意图:调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件。(如上面的代码)
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。
2,intentService是继承于Service并处理异步请求的一个类,其中在onHandleIntent方法中执行仅有的一个工作线程,当有多个线程请求时,会以队列的方式,进行排队,
当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。而Service执行耗时操作需要手动开启线程,并在用完一以后又要手动关闭线程
3,PendingIntent(等待的,未决定的Intent),主要用来在某个事件完成后执行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所属程序结束,PendingIntent依然有效,可以在其他程序中使用
如:
//1,启动一个Service,当intent结束了,这时pendingintent还是存在的,
 Intent intent= new Intent(this,ClubIntentService.class);
  intent.putExtra("roomName", roomName);
  intent.putExtra("name", name);
  //使用PendingIntent
  int requestCode=100;//由于Pending中的第三种操作
Intent data=new Intent();//这个data的有点别扭,这时一个Intent对象。
PendingIntent pendingIntent=createPendingResult(requestCode, data, 0);
intent.putExtra("pendingIntent", pendingIntent);//将pendingIntent放入Intent中一起,传递到IntentService中
  startService(intent);
2,//到IntentService中Intent其实已经销毁了,当PendingIntent,还是存在的,
                             PendingIntent pendingIntent = intent.getParcelableExtra("pendingIntent");
int resultcode = 200;
Intent data = new Intent();
//
data.putExtra("status", status);
pendingIntent.send(this, resultcode, data);//发送消息,给UI线程更新
3,//创建onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==100&&resultCode==200){//比较请求码和返回码
int  status=data.getIntExtra("status", -1);//得到PendingIntent中传递过来的数据
if(status==0){
Toast.makeText(this,"go to room success", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,ChatActivity_.class);
startActivity(intent);
}else{
Toast.makeText(this,"go to room fail", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
0 0
原创粉丝点击