Android进程间通信

1.使用Handler传递消息
handler 大家可以把它想象成主线程(UI线程)的一个子线程,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑,handler 是一个子线程所以它的耗时操作不会阻塞主线程,大家都知道在android的开发中如果代码中某个地方阻塞主线程超过5秒的话系统会提示ANR (系统提示强制关闭)所以在耗时操作上我们可以考虑开启一个子线程避免ANR。 handler会向主线程发送消息 会以队列的形式排列着配合等待主线程更新UI 逻辑 等等。
handler的使用
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {

    Bundle bundle= msg.getData();
    //通过key的名称拿到它的值
    String  number = bundle.getString("number");
    //msg.what为handler接收到的消息编号
    switch(msg.what) {
    case UPDATE_TIME:
    mTextView.setText("正在更新时间" + number);
    break;
    case UPDATE_COMPLETED:
    mTextView.setText("更新完毕");
    break;
    }
    super.handleMessage(msg);
}
};

@Override
public void run() {

while (mRunning) {
    try {
    mShowNumber++;
    /** 把须要的数据放入bandle中 **/
    Bundle bandle = new Bundle();
    bandle.putString("number", String.valueOf(mShowNumber));

    /** 设置这条信息的编号为更新时间 **/
    /** 将bandle写入message中 **/
    /** 最后将这个message发送出去 **/
    /** mShowNumber小于10更新时间 否则更新完毕 **/
    Message msg = new Message();
    if(mShowNumber <=10) {
        msg.what = UPDATE_TIME;
    }else {
        mRunning = false;
        msg.what = UPDATE_COMPLETED;
    }
    msg.setData(bandle);
    handler.sendMessage(msg);
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
}

2.Notifation通知栏信息
Notifation通知栏会在屏幕上方向用户提示信息 但是不会打断用户正在阅读的内容,除非用户手动将 Notifation通知栏拉下。 Notifation的好处就是在于不会影响用户的操作,比如用户正在阅读非常重要的信息这时候帮他直接打开一个activity会非常不合适 因为直接影响到了他当时的操作行为 所以Notifation就出来了。建议大家在开发中遇到可能打断用户使用的情况下都去使用Notifation通知栏。
3.public class NotificationActivity extends Activity {
NotificationManager mManager = null;
Notification notification =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.notification);

// 得到通知消息的管理器对象,负责管理 Notification 的发送与清除消息等
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 创建Notification对象 参数分别代表 通知栏 中显示的图标 显示的标题 显示的时间
notification = new Notification(R.drawable.jay,
    "Android专业开发群", System.currentTimeMillis());

// 设置在通知栏中点击后Notification自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL;

//设置点击后转跳的新activity
Intent intent = new Intent(this, MyShowActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP¦ Intent.FLAG_ACTIVITY_NEW_TASK);

//通过bundle可以带一些数据过去 这里将字符串传递了过去
Bundle bundle = new Bundle();
bundle.putString("name", "从Notification转跳过来的");
intent.putExtras(bundle);

//设置通知栏中显示的内容
PendingIntent contentIntent = PendingIntent.getActivity(this,
    R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "Android专业开发群",
    "QQ群号 164257885", contentIntent);

Button button0 = (Button)findViewById(R.id.button0);
button0.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    //打开这个Notification通知
    mManager.notify(0, notification);
    }
});

Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    //关闭这个Notification通知
    mManager.cancelAll();
    }
});

super.onCreate(savedInstanceState);
}

}
3.广播的发送与接收

Android开发中如果须要对两个完全没关系的程序之间进行通信 就可以使用发送广播与接收广播的机制来实现 ,例如程序A发送了一个广播 程序B接受到 做一些事情 这样就达到了相互的通讯。

3.Activity与Activity之间的转跳

在软件应用的开发中肯定会有多个Activity 这样它们之间就会存在相互转跳的关系 转跳的实现方式还是使用Intent 然后startActivity ,当然转跳的话是可以带数据过去的。比如从A跳到B 可以把A中的一些数据通过Intent传递给B 。

Android研究院之应用程序通信(十) - 雨松MOMO程序研究院 - 6

   读下面这段代码 大家会发现intent与bandle 传递数值的方式基本一样为什么还要分成两个呢? 确实他们两个传递的数值的方式非常类似, 他们两个的区别就是Intent属于把零散的数据传递过去 而bundle则是把零散的数据先放入bundle 然后在传递过去。我举一个例子 比如我们现在有3个activity  A.B.C  须要把A的数据穿给B然后在穿给C ,如果使用intent一个一个传递 须要在A类中一个一个传递给B 然后B类中获取到所有数值  然后在一个一个传递给C  **这样很麻烦 但是 如果是bundle的话 B类中直接将bundler传递给C 不用一个一个获得具体的值  然后在C类中直接取得解析数值。**

传递

    /**Activity之间传递值**/
    Button botton3 = (Button)findViewById(R.id.button3);
    botton3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
     Intent intent = new Intent(mContext,ShowActivity.class);
     //使用intent.putExtra()直接传递
     intent.putExtra("name", "雨松MOMO");
     intent.putExtra("age", 25);
     intent.putExtra("boy", true);

     //把数值放进bundle 然后在把整个bundle通过intent.putExtra()传递
     Bundle bundle = new Bundle();
     bundle.putString("b_name", "小可爱");
     bundle.putInt("b_age", 23);
     bundle.putBoolean("b_boy", false);
     //在这里把整个bundle 放进intent中
     intent.putExtras(bundle);
     //开启一个新的 activity 将intent传递过去
     startActivity(intent);
    }
});

接收

public class ShowActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.my);

Intent intent = getIntent();

String name = intent.getStringExtra("name");
//第二个参数为默认值 意思就是如果在intent中拿不到的话
//就用默认值
int age  = intent.getIntExtra("age", 0);
boolean isboy = intent.getBooleanExtra("boy", false);
TextView textView0 = (TextView)findViewById(R.id.text0);

textView0.setText("姓名  " + name + "年龄 " + age + "男孩?  " + isboy);

Bundle bundle = intent.getExtras();
name = bundle.getString("b_name");
//第二个参数为默认值 意思就是如果在bundle中拿不到的话
//就用默认值
age = bundle.getInt("b_age",0);
isboy = bundle.getBoolean("b_boy", false);

TextView textView1 = (TextView)findViewById(R.id.text1);

textView1.setText("姓名  " + name + "年龄 " + age + "男孩?  " + isboy);

super.onCreate(savedInstanceState);
}

}
最后还是那句老话如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。

//Serializeable传递对象的方法
public void SerializeMethod(){
Person mPerson = new Person();
mPerson.setName(“frankie”);
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);

    startActivity(mIntent);  
}  
//Pacelable传递对象方法  
public void PacelableMethod(){  
    Book mBook = new Book();  
    mBook.setBookName("Android Tutor");  
    mBook.setAuthor("Frankie");  
    mBook.setPublishTime(2010);  
    Intent mIntent = new Intent(this,ObjectTranDemo2.class);  
    Bundle mBundle = new Bundle();  
    mBundle.putParcelable(PAR_KEY, mBook);  
    mIntent.putExtras(mBundle);  

    startActivity(mIntent);  
}  

信息接受方
ackage com.tutor.objecttran;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    TextView mTextView = new TextView(this);  
    Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);  
    mTextView.setText("You name is: " + mPerson.getName() + "/n"+  
            "You age is: " + mPerson.getAge());  

    setContentView(mTextView);  
}  

}

ObjectTranDemo2.java:

view plaincopy to clipboardprint?
package com.tutor.objecttran;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo2 extends Activity {

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    TextView mTextView = new TextView(this);  
    Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);  
    mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+  
                      "Author is: " + mBook.getAuthor() + "/n" +  
                      "PublishTime is: " + mBook.getPublishTime());  
    setContentView(mTextView);  
}  

}

4.Content Provider 也属于进程间信息交互
5.1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
icp:interprocess communication :内部进程通信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值