zoukankan      html  css  js  c++  java
  • 旺仔:有序广播

    package com.example.administrator.test.Fragment.Broadcast;
    
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    import com.example.administrator.test.R;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
        }
        public void bt1_OnClick(View v)
        {
            //一般广播
            Intent intent=new Intent( "Broadcast.MainActivity" );
            intent.putExtra( "name","广播发送测试" );
            sendBroadcast( intent );
            Toast.makeText( this, "一般广播已经发出去了", Toast.LENGTH_SHORT ).show();
        }
        //动态注册
        MyReceiver mr;
        public void bt2_OnClick(View v)
        {
            mr=new MyReceiver();
            IntentFilter interFilter=new IntentFilter( "Broadcast.MainActivity1" );
           // interFilter.setPriority( 1000 );
            registerReceiver( mr,interFilter );
    
        }
    
        //发送有序广播
        public void bt3_OnClick(View v)
        {
            Intent intent=new Intent( "Broadcast.MainActivity" );
            intent.putExtra( "name","广播发送测试" );
            sendOrderedBroadcast( intent ,null);//第二个参数叫权限字符串,一般设为null
            Toast.makeText( this, "有序广播已经发出去了", Toast.LENGTH_SHORT ).show();
        }
        //反注册
        @Override
        protected void onDestroy() {
            if (mr!=null)
            {
                unregisterReceiver( mr );
            }
            super.onDestroy();
        }
    }
    package com.example.administrator.test.Fragment.Broadcast;
    
            import android.content.BroadcastReceiver;
            import android.content.Context;
            import android.content.Intent;
            import android.util.Log;
            import android.widget.Toast;
    
    public class MyReceiver extends BroadcastReceiver {
        public MyReceiver() {
            Log.e( "TAG","MyReceiver被实例化" );
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //阻断有序广播接收
            if (isOrderedBroadcast())
            {
               // abortBroadcast();
            }
            String str=intent.getStringExtra( "name" );
            intent.putExtra( "name1","这是修改的内容" );
            Toast.makeText( context, "MyReceiver收到的广播="+str, Toast.LENGTH_SHORT ).show();
        }
    }
    package com.example.administrator.test.Fragment.Broadcast;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MyReceiver2 extends BroadcastReceiver {
        public MyReceiver2() {
            Log.e("TAG","MyReceiver被实例化");
    
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String str=intent.getStringExtra( "name1" );
            Toast.makeText( context, "MyReceive2收到的广播="+str, Toast.LENGTH_SHORT ).show();
        }
    }
     <activity android:name=".Fragment.Broadcast.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
    
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
    
            <receiver
                android:name=".Fragment.Broadcast.MyReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="100">
                    <action android:name="Broadcast.MainActivity"/>
                </intent-filter>
            </receiver>
            <receiver
                android:name=".Fragment.Broadcast.MyReceiver2"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="100">
                    <action android:name="Broadcast.MainActivity"/>
                </intent-filter>
            </receiver>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
    >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送一般广播"
            android:onClick="bt1_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="动态注册广播接收器"
            android:onClick="bt2_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送有序广播"
            android:onClick="bt3_OnClick"/>
    </LinearLayout>
  • 相关阅读:
    ubuntu下查看环境变量
    ubuntu关闭自动更新、打开 ubuntu 的 apport 崩溃检测报告功能
    Ubuntu 配置AP总结
    ubuntu 12.04亮度无法调节和无法保存屏幕亮度解决办法(echo_brightness)
    Ubuntu 13.04 双显卡安装NVIDIA GT 630M驱动
    Linux下添加硬盘,分区,格式化详解
    Eclipse启动分析
    “蚁族” 的生活方式画像
    Ubuntu下的防火墙
    Ubuntu下的杀毒
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5730074.html
Copyright © 2011-2022 走看看