-----------------------------------------1.广播代码---------------------
广播BroadcastReceiver:传递数据
使用步骤:
一、发送
//1.开启意图Intent
Intent intent = new Intent();
//2.向Intent中添加需要传递的数据
intent.putExtra("content", "放假回来第一天就要上课,内心是拒绝的!");
//3.设置发送的广播的频道(action)
intent.setAction(action);
//4.1发送无序广播
sendBroadcast(intent);
//4.2发送有序广播
//sendOrderedBroadcast(intent, null);
【补充】有序广播和无序广播可以被所有的广播(action相同)接收到,但是无序广播不能被终止abortBroadcast();
二、接收
自己写一个类(不要写成内部类) extends BroadcastReceiver
然后重写 onReceive(Context context, Intent intent)方法
接下来就可以在该方法intent中获取传递的数据,或者终止广播abortBroadcast();【前提是该广播是有序的】
三、配置(注册广播)
方式一:静态注册
<receiver
android:name="com.example.ruanjian_broadcast.MyReciver1">
<intent-filter android:priority="1000">
<action android:name="com.example.ruanjian_broadcast.send" />
</intent-filter>
</receiver>
注意:<action android:name="com.example.ruanjian_broadcast.send" />里面的值要和发送广播的频道action一致的。
如果需要提高某个广播接收时的优先级,在intent-filter添加属性android:priority="1000",取值范围-1000到1000
方式二:动态注册:
//创建广播对象
MyReciver2 reciver2 = new MyReciver2();
//模拟一遍静态注册
//准备好过滤器
IntentFilter filter = new IntentFilter();
//为过滤器添加action
filter.addAction(action);
//注册广播,并且为广播添加对应的过滤器
registerReceiver(reciver2, filter);
注意:一般情况下使用动态注册广播时,需要自己手动的注销:unregisterReceiver(reciver2);
protected void onDestroy() {
super.onDestroy();
//如果是动态注册的广播,需要注销
unregisterReceiver(reciver2);
}
----------------------------------------------------------上面是要考的,下面的代码是所有--------------------
package com.example.wulian_broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReciver2 extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//接收到广播,并取出数据
String content = intent.getStringExtra("content");
Toast.makeText(context, "MyReciver2:"+content, Toast.LENGTH_SHORT).show();
}
}
package com.example.wulian_broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReciver1 extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//接收到广播,并取出数据
//当发送过来的是有序广播时,才能终止
//abortBroadcast();
String content = intent.getStringExtra("content");
Toast.makeText(context, "MyReciver1:"+content, Toast.LENGTH_SHORT).show();
}
}
package com.example.wulian_broadcast;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Filter;
/**
* MyReciver1:采用的静态注册的方式
* MyReciver2:采用的动态态注册的方式(模拟静态注册的方式即可)
*/
public class MainActivity extends Activity {
private final String ACTION = "com.example.wulian_broadcast.send";
private Button bt_send;
private MyReciver2 reciver2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态的注册MyReciver2:
//1.先创建出MyReciver2广播对象
reciver2 = new MyReciver2();
//2.使用意图过滤器intent-filter
IntentFilter intentFilter = new IntentFilter();
//3.向intent中添加action
intentFilter.addAction(ACTION);
//4.注册广播MyReciver2,同时绑定对应的过滤器
registerReceiver(reciver2, intentFilter);
bt_send = (Button) this.findViewById(R.id.bt_send);
bt_send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//执行发送广播的操作
//1.准备一个intent
Intent intent = new Intent();
//2.向intent中添加要传递的数据
intent.putExtra("content", "今天周一,还有四天就又可以休息了!");
//3.设置发送广播的频道
intent.setAction(ACTION);
//4.发送一个广播:1.无序广播 2.有序广播
//4.1无序广播(此时设置广播的优先级是没有效果的,同时不能终止广播)
sendBroadcast(intent);
//4.2发送有序广播
//sendOrderedBroadcast(intent, null);
}
});
Button bt2 = (Button) this.findViewById(R.id.bt_2);
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
/*因为MyReciver1采用的是静态注册也称为常驻广播,不需要我们自己注销,
但是MyReciver2采用的动态的注册的方式,只有当Activity运行的时候才有用,
所以当Activity销毁时,需要把MyReciver2注销掉防止内存泄露问题。*/
protected void onDestroy() {
super.onDestroy();
Log.e("onDestroy", "onDestroy");
unregisterReceiver(reciver2);
}
@Override
protected void onStop() {
super.onStop();
Log.e("onStop", "onStop");
unregisterReceiver(reciver2);
}
}
-------------------------------------------------------2.Handle代码---------------------------------------
bt_send = (Button) this.findViewById(R.id.bt_send);
bt_send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//执行发送广播的操作
//1.准备一个intent
Intent intent = new Intent();
//2.向intent中添加要传递的数据
intent.putExtra("content", "今天周一,还有四天就又可以休息了!");
//3.设置发送广播的频道
intent.setAction(ACTION);
//4.发送一个广播:1.无序广播 2.有序广播
//4.1无序广播(此时设置广播的优先级是没有效果的,同时不能终止广播)
sendBroadcast(intent);
//4.2发送有序广播
//sendOrderedBroadcast(intent, null);
}
});
Button bt2 = (Button) this.findViewById(R.id.bt_2);
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
/*因为MyReciver1采用的是静态注册也称为常驻广播,不需要我们自己注销,
但是MyReciver2采用的动态的注册的方式,只有当Activity运行的时候才有用,
所以当Activity销毁时,需要把MyReciver2注销掉防止内存泄露问题。*/
protected void onDestroy() {
super.onDestroy();
Log.e("onDestroy", "onDestroy");
unregisterReceiver(reciver2);
}
@Override
protected void onStop() {
super.onStop();
Log.e("onStop", "onStop");
unregisterReceiver(reciver2);
}
}
----------------------------------------------------------2Handler 消息机制
Handler 消息机制:
开发过程中,有时我们需要在创建的子线程中处理一些耗时的操作,然后再把处理的结果更新到UI界面上
android系统不能够在子线程中操作UI界面,提供一个消息处理机制Handler机制
两种方式:
方式一:采用send方式
在子线程中 ,先准备好要发送的Message对象,然后msg对象进行数据的绑定,
再使用handler对象的sendMessage(msg)
//准备好消息
Message msg = new Message();
msg.what = 1;
msg.arg1 = count;
//使用handler发送消息
handler.sendMessage(msg);
最后需要在handler中处理接收到的消息:重写handler中的handleMessage(Message msg)
handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
//
if (msg.what==1){
tv_show.setText(""+msg.arg1);
}
}
};
方式二:post方式,相当于使用handler的post方法告诉UI线程我将要在updateRunnable中操作UI
//立即发送
handler.post(updateRunnable);
//延迟发送
handler.postDelayed(updateRunnable, 2000);
此时,需要有一个Runnable(一个子线程)
创建Runnable updateRunnable,重写run方法
Runnable updateRunnable = new Runnable() {
public void run() {
if(count++<30){
tv_show.setText(""+count);
//循环的更新
// handler.post(updateRunnable);
handler.postDelayed(updateRunnable, 2000);
}
}
};
Andriod期末总结
最新推荐文章于 2023-11-29 12:12:42 发布