• BroadcastReceiver的两种注册方式之------动态注册


    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="24dip"
            android:gravity="center"
            android:text="BroadcastReceiver演示"/>
        
           <Button
            android:id="@+id/send_dynamic"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="发送自定义动态注册广播" />
    
    </LinearLayout>

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.my.broaccast"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    
    </manifest>

    MainActivity.java

    package com.my.broaccast;
    
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        
        private Button sendDynamicButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);    
            
            sendDynamicButton = (Button) findViewById(R.id.send_dynamic);
            sendDynamicButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent();
                    intent.setAction("com.my.dynamicrecever");
                    intent.putExtra("msg", "接收动态注册广播成功!");
                    sendBroadcast(intent);
                }
            });
        }
        
        protected void onStart() {
            super.onStart();
            IntentFilter dynamic_filter = new IntentFilter();
            dynamic_filter.addAction("com.my.dynamicrecever");
            registerReceiver(dynamicReceiver, dynamic_filter);
        }
        
        private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
            
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                if (intent.getAction().equals("com.my.dynamicrecever")) {
                    String msg = intent.getStringExtra("msg");
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }
        };
    }
  • 相关阅读:
    JavaWeb(二)会话管理之细说cookie与session
    JavaWeb(一)Servlet中乱码解决与转发和重定向的区别
    JavaWeb(一)Servlet中的request与response
    JavaWeb(一)Servlet中的ServletConfig与ServletContext
    JavaWeb(一)之细说Servlet
    OOAD-设计模式(一)概述
    异常处理升级版
    MySQL优化原理
    hadoop 有那些发行版本
    centos7 安装搜狗输入法
  • 原文地址:https://www.cnblogs.com/xunbu7/p/4801309.html
Copyright © 2020-2023  润新知