四大组件BroadCastReceiver运用-广播接受者

BroadCastReceiver运用-广播接受者

1,注册一个广播

package com.example.day013;

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

java代码

package com.example.day013;

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

BroadCastReceiver和Notifcation

BroadCastReceiver和Notifcation
1,注册一个广播.(静态或者动态都可以.代码省略)
2,广播的内容.

package com.example.day013;

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

    }
}

使用BroadCastReceiver发送消息更新UI

布局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_ui_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>

Activity代码

package com.example.day013;

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 Main2Activity extends AppCompatActivity {
    private Button updataUiId;
    private ImageView imageId;
    private MyReceiver2 myReceiver2;

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

        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(myReceiver2,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(myReceiver2);
    }
}

广播接受这的代码

package com.example.day013;

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);
                }
            });
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值