赞
踩
发送的广播事件能被其他应用程序获取,也可以响应其他应用程序发送的广播事件(可以通过 exported–是否监听其他应用程序发送的广播 ) 全局广播可以动态注册,也可以静态注册。
广播事件不被其他应用程序获取,也不能响应其他应用程序发送的广播事件。
本地广播只能动态注册,不能静态注册。动态注册或方法时需要用到LocalBroadcastManager。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: ");
}
}
import android.content.Intent; import android.content.IntentFilter; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button send; private LocalBroadcastManager localBroadcastManager; private MyReceiver myReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send = findViewById(R.id.send); send.setOnClickListener(this); //创建一个本地广播 localBroadcastManager = LocalBroadcastManager.getInstance(this); //动态注册一个广播 myReceiver = new MyReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.feng"); localBroadcastManager.registerReceiver(myReceiver,intentFilter); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.send: Intent intent = new Intent(); intent.setAction("com.feng"); //注意发送者 localBroadcastManager.sendBroadcast(intent); break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); //销毁 localBroadcastManager.unregisterReceiver(myReceiver); } }
可以明确的知道正在发送的广播不会离开我们的程序,不用担心机密数据泄露
其他程序无法将广播发送到我们的程序内部,不需要担心安全问题
发送本地广播比系统全局的广播效率高
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceive: "); Notification.Builder builder = new Notification.Builder(context); builder.setAutoCancel(true); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentText("这是标题"); builder.setContentText("这是内容"); //弹出 builder.setPriority(Notification.PRIORITY_MAX); builder.setDefaults(Notification.DEFAULT_ALL); //跳转 Intent intent1 = new Intent(context,Main2Activity.class); PendingIntent activity = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(activity); Notification build = builder.build(); NotificationManager manager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1,build); } }
xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main2Activity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/updata_id" android:text="更新图片" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image_id" android:src="@mipmap/ic_launcher" /> </LinearLayout>
import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private Button updataUiId; private ImageView imageId; private MyReceiver myReceiver; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); updataUiId = findViewById(R.id.updata_ui_id); imageId = findViewById(R.id.image_id); myReceiver2 = new MyReceiver2(handler,imageId); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.feng.ui"); registerReceiver(myReceiver,intentFilter); updataUiId.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("com.feng.ui"); sendBroadcast(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(myReceiver); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.widget.ImageView; public class MyReceiver2 extends BroadcastReceiver { private Handler handler; private ImageView imageView; public MyReceiver2(Handler handler, ImageView imageView) { super(); this.handler = handler; this.imageView = imageView; } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("com.feng.ui".equals(action)){ handler.post(new Runnable() { @Override public void run() { imageView.setImageResource(R.mipmap.ic_launcher_round); } }); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。