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);
}

更多相关文章

  1. Android中线程的处理
  2. android.content
  3. 活用Android的Message Queue(1)
  4. Android(安卓)组件系列-----Activity的传值和回传值
  5. Android多线程之HandlerThread
  6. Android(安卓)自定义View控件,实现跟随手指触摸移动的小球
  7. Android中inflate简介
  8. android异步处理Handler+Thread使用进阶
  9. Android(安卓)消息机制问题总结

随机推荐

  1. 编译Android 使用 Java5 还是 Java6
  2. RecyclerView详解(二):ItemDecoration使用(k
  3. java.lang.ClassCastException:org.apach
  4. android studio 编译后 app-debug.apk 和
  5. android.support.v4.app.Fragment和andro
  6. Android之使用HttpURLConnection进行网络
  7. Android这可能是当今最火,功能最全的快速
  8. Android(安卓)FloatingActionButton: Flo
  9. 一个简单android的全选,单选demo
  10. Android studio下载与安装