zoukankan      html  css  js  c++  java
  • Android之BroadcastReceiver1

    1.触发发送广播

    public class MainActivity extends Activity {
        private Button sendButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sendButton = (Button) findViewById(R.id.sendButton);
            sendButton.setOnClickListener(new BroadcastListener());
            /*
             * if (savedInstanceState == null) {
             * getSupportFragmentManager().beginTransaction() .add(R.id.container,
             * new PlaceholderFragment()).commit(); }
             */
        }
    
        class BroadcastListener implements OnClickListener {
            @Override
            public void onClick(View v) {
                TestReceiver tr = new TestReceiver();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_EDIT);
                MainActivity.this.sendBroadcast(intent);
            }
        }

    2.注册广播

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mars_1700_broadcast"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.mars_1700_broadcast.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>
            <!-- 注册一个广播 -->
            <receiver android:name=".TestReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.EDIT"/>
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>

    3.定义接收广播类

    package com.example.mars_1700_broadcast;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class TestReceiver extends BroadcastReceiver{
    
        public TestReceiver(){
            System.out.println("TestReceiver");
        }
        
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("onReceive");
        }
    
    }
  • 相关阅读:
    select应用于read函数 超时非阻塞方式
    取文本索引所执向的值(简单)
    linux c函数指针的应用
    解决vsftp无法启动问题(转)
    Could not chdir to home directory /home/USER: Permission denied
    sscanf和正则表达式
    存储过程重置SEQUENCE值从新开始。
    Signal ()函数详细介绍 Linux函数(转)
    linux 环境NTP配置与开机自启动(转)
    linux下iconv()函数的用法(转载并修改)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3649971.html
Copyright © 2011-2022 走看看