android本地广播

原创 2015年11月19日 13:57:01
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.example.android.supportv4.content;


import com.example.android.supportv4.R;


import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.ServiceCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


/**
 * Demonstrates the use of a LocalBroadcastManager to easily communicate
 * data from a service to any other interested code.
 */
public class LocalServiceBroadcaster extends Activity {
    static final String ACTION_STARTED = "com.example.android.supportv4.STARTED";
    static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE";
    static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED";


    LocalBroadcastManager mLocalBroadcastManager;
    BroadcastReceiver mReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.local_service_broadcaster);


        // This is where we print the data we get back.
        final TextView callbackData = (TextView)findViewById(R.id.callback);


        // Put in some initial text.
        callbackData.setText("No broadcast received yet");


        // We use this to send broadcasts within our local process.
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);


        // We are going to watch for interesting local broadcasts.
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_STARTED);
        filter.addAction(ACTION_UPDATE);
        filter.addAction(ACTION_STOPPED);
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(ACTION_STARTED)) {
                    callbackData.setText("STARTED");
                } else if (intent.getAction().equals(ACTION_UPDATE)) {
                    callbackData.setText("Got update: " + intent.getIntExtra("value", 0));
                } else if (intent.getAction().equals(ACTION_STOPPED)) {
                    callbackData.setText("STOPPED");
                }
            }
        };
        mLocalBroadcastManager.registerReceiver(mReceiver, filter);


        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.start);
        button.setOnClickListener(mStartListener);
        button = (Button)findViewById(R.id.stop);
        button.setOnClickListener(mStopListener);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocalBroadcastManager.unregisterReceiver(mReceiver);
    }


    private OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(LocalServiceBroadcaster.this, LocalService.class));
        }
    };


    private OnClickListener mStopListener = new OnClickListener() {
        public void onClick(View v) {
            stopService(new Intent(LocalServiceBroadcaster.this, LocalService.class));
        }
    };


    public static class LocalService extends Service {
        LocalBroadcastManager mLocalBroadcastManager;
        int mCurUpdate;


        static final int MSG_UPDATE = 1;


        Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_UPDATE: {
                        mCurUpdate++;
                        Intent intent = new Intent(ACTION_UPDATE);
                        intent.putExtra("value", mCurUpdate);
                        mLocalBroadcastManager.sendBroadcast(intent);
                        Message nmsg = mHandler.obtainMessage(MSG_UPDATE);
                        mHandler.sendMessageDelayed(nmsg, 1000);
                    } break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };


        @Override
        public void onCreate() {
            super.onCreate();
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        }


        public int onStartCommand(Intent intent, int flags, int startId) {
            // Tell any local interested parties about the start.
            mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED));


            // Prepare to do update reports.
            mHandler.removeMessages(MSG_UPDATE);
            Message msg = mHandler.obtainMessage(MSG_UPDATE);
            mHandler.sendMessageDelayed(msg, 1000);
            return ServiceCompat.START_STICKY;
        }


        @Override
        public void onDestroy() {
            super.onDestroy();


            // Tell any local interested parties about the stop.
            mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED));


            // Stop doing updates.
            mHandler.removeMessages(MSG_UPDATE);
        }


        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}

Android自定义广播

  • 2016年07月22日 16:27
  • 977KB
  • 下载

Android广播

  • 2015年08月28日 13:43
  • 3.16MB
  • 下载

android6.0 外部存储设备插拔广播以及获取路径(U盘)

这里我将U盘作为例子来说明解析。android4.1版本时U盘插拔时的广播在android4.1时我们可以用以下的BroadcastReceiver接收U盘广播。 ...

android短信的获取非广播方式可用

  • 2015年07月31日 17:55
  • 2.72MB
  • 下载

android广播

  • 2013年10月26日 09:55
  • 101KB
  • 下载

玩转Android---组件篇---Broadcast Receiver(广播接收器)

原址:http://hualang.iteye.com/category/143855 Braodcast Receiver顾名思义就是广播接收器,它和时间处理机制类似,但是事件处理机制是程序组...

Android卸载应用和安装应用的广播

  • 2015年11月24日 17:11
  • 1.23MB
  • 下载

Android广播demo

  • 2016年01月06日 11:48
  • 631KB
  • 下载

android 通过广播来弹出dialog(在小米上会一闪而过的原因)

最近又学了一些android 的基础知识广播,之前因为一直没有用到所以就没怎么重视,在接触一些大牛们的项目源码后,发现广播确是蛮有用的。 于是自己就翻阅了几本书和几个相关视频,仿照着写了一个基本功能...

android广播简单使用

  • 2016年01月04日 09:47
  • 2.87MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android本地广播
举报原因:
原因补充:

(最多只允许输入30个字)