zoukankan      html  css  js  c++  java
  • past object with intent

    1. BackGround
      When I learned how to invoke one object's callback function, I found that the callback has three category:

      First: in the local, who define one function in file, then give it to you, you get it and use it.

      Second: You can use the intent to past one object to the others, then the others get the object and use it's method. This is called You define, I use.

      Third: Use binder, binder can be pasted with different processes. And in binder, you can define method, then past the binder to others, then others get the binder and use its methods.
    2. Sample Code//the class of being past object
       1 package com.example.ddd;
       2 
       3 import android.os.Parcel;
       4 import android.os.Parcelable;
       5 //must implements Parcelable and could not implements other interface. Not like zhangsan implements what, Parcelable
       6 public class zhangsan implements Parcelable{
       7 
       8     public String getName() {
       9         // TODO Auto-generated method stub 
      10         return "zhangsan";
      11     }
      12 
      13     public void setName() {
      14         // TODO Auto-generated method stub
      15         
      16     }
      17 
      18     @Override
      19     public int describeContents() {
      20         // TODO Auto-generated method stub
      21         return 0;
      22     }
      23 
      24     @Override
      25     public void writeToParcel(Parcel dest, int flags) {
      26         // TODO Auto-generated method stub
      27         
      28     }
      29     public static final Parcelable.Creator<zhangsan> CREATOR = new Creator<zhangsan>() {  
      30         public zhangsan createFromParcel(Parcel source) {  
      31             zhangsan mBook= new zhangsan();  
      32             return mBook;  
      33         }  
      34         public zhangsan[] newArray(int size) {  
      35             return new zhangsan[size];  
      36         }  
      37     };
      38 
      39 }

      //create the BroadcastReceiver

       1 package com.example.binderservice;
       2 
       3 import android.content.BroadcastReceiver;
       4 import android.content.Context;
       5 import android.content.Intent;
       6 import android.util.Log;
       7 
       8 import com.example.ddd.zhangsan;
       9 
      10 public class MyReceiver extends BroadcastReceiver{
      11 
      12     @Override
      13     public void onReceive(Context context, Intent intent) {
      14         // TODO Auto-generated method stub
      15         Log.e("receiver","in receiver");
      16         zhangsan zhang =(zhangsan)intent.getParcelableExtra("object");//"object is a string which map to past object"
      17         Log.e("receiver",zhang.getName());19     }
      20 
      21 }

      //setBroadcast

       1 package com.example.interfacep;
       2 
       3 import android.os.Bundle;
       4 import android.app.Activity;
       5 import android.content.Intent;
       6 import android.view.Menu;
       7 
       8 import com.example.ddd.zhangsan;
       9 
      10 public class MainActivity extends Activity {
      11 
      12     @Override
      13     protected void onCreate(Bundle savedInstanceState) {
      14         super.onCreate(savedInstanceState);
      15         setContentView(R.layout.activity_main);
      16         zhangsan zhang = new zhangsan();
      17         Intent i = new Intent("intent");//"intent" is the intent.action name, in the receiver project AndroidManifest.xml,receiver <intent-filter> <action android:name="intent" /> 
      18         Bundle extras = new Bundle();
      19         extras.putParcelable("object", zhang);//"object is a string which map to the past object"
      20         i.putExtras(extras);
      21         sendBroadcast(i);
      22     }
      23 
      24     @Override
      25     public boolean onCreateOptionsMenu(Menu menu) {
      26         // Inflate the menu; this adds items to the action bar if it is present.
      27         getMenuInflater().inflate(R.menu.activity_main, menu);
      28         return true;
      29     }
      30 
      31 }
  • 相关阅读:
    EntityFramework之创建数据库及基本操作(一)
    Entity Framework Code First (八)迁移 Migrations
    [转]Tomcat启动报错:AnnotationConfigBeanDefinitionParser are only available on JDK 1.5 and higher
    [转]bootstrap table本地数据使用方法
    [转]MySQL-5.7 Update语句详解
    [转]操作MySQL数据库报出:Parameter index out of range (1 > number of parameters, which is
    [转]在MySQL中创建实现自增的序列(Sequence)的教程
    [转]MySQL如何设置自动增长序列 SEQUENCE
    [转]Mustache 使用心得总结
    [转]关于重定向RedirectAttributes的用法
  • 原文地址:https://www.cnblogs.com/alexjie/p/2974901.html
Copyright © 2011-2022 走看看