zoukankan      html  css  js  c++  java
  • 安卓软件学习进度_13

    广播自定义意图

    如果你想要应用程序中生成并发送自定义意图,你需要在活动类中通过sendBroadcast()来创建并发送这些意图。如果你使用sendStickyBroadcast(Intent)方法,则意图是持久的(sticky),这意味者你发出的意图在广播完成后一直保持着。

    public void broadcastIntent(View view)
    {
       Intent intent = new Intent();
       intent.setAction("com.runoob.CUSTOM_INTENT");
       sendBroadcast(intent);
    }

    com.runoob.CUSTOM_INTENT的意图可以像之前我们注册系统产生的意图一样被注册。

    <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <receiver android:name="MyReceiver">
    
          <intent-filter>
             <action android:name="com.runoob.CUSTOM_INTENT">
             </action>
          </intent-filter>
    
       </receiver>
    </application>

    实例

    这个实例将解释如何创建广播接收器来拦截自定义意图。一旦你熟悉自定义意图,你可以为应用程序编程来拦截系统产生的意图。让我们按照下面的步骤来修改Hello World实例章节中我们创建的Android应用程序。

    步骤描述
    1 使用Android Studio来创建Android应用程序并命名为broadcastreceiver,并放在Hello World实例章节中的com.runoob.broadcastreceiver包下。
    2 修改主要活动文件MainActivity.java来添加broadcastIntent()方法。
    3 在com.runoob.broadcastreceiver包下创建名为MyReceiver.java的新的Java文件来定义广播接收器。
    4 应用程序可以处理一个或多个自定义或者系统的意图,没有任何限制。每个你想拦截的意图都需要使用<receiver.../>标签在AndroidManifest.xml中注册。
    5 修改res/layout/activity_main.xml文件中的默认内容来包含一个广播意图的按钮。
    6 不需要修改字符串文件,Android Studio会注意string.xml文件。
    7 启动Android模拟器来运行应用程序,并验证应用程序所做改变的结果。

    下面是修改的主要活动文件src/com.runoob.broadcastreceiver/MainActivity.java的内容。这个文件包含了每个基础的生命周期方法。我们添加了broadcastIntent()方法来广播自定义事件。

    package com.runoob.broadcastreceiver;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.content.Intent;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        // 广播自定义意图
        public void broadcastIntent(View view){
            Intent intent = new Intent();
            intent.setAction("cn.programmer.CUSTOM_INTENT");
            sendBroadcast(intent);
        }
    }

    下面是src/com.runoob.broadcastreceiver/MyReceiver.java的内容:

    package com.runoob.broadcastreceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "检测到意图。", Toast.LENGTH_LONG).show();
        }
    }

    接下来修改AndroidManifest.xml文件。这里通过添加<receiver.../>标签来包含我们的广播接收器:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.runoob.broadcastreceiver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="22" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
    
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
    
            </activity>
    
            <receiver android:name="MyReceiver">
    
                <intent-filter>
                    <action android:name="cn.programmer.CUSTOM_INTENT">
                    </action>
                </intent-filter>
    
            </receiver>
    
        </application>
    
    </manifest>

    下面是res/layout/activity_main.xml文件的内容,包含广播自定义意图的按钮。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="广播实例"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textSize="30dp" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="www.runoob.com"
            android:textColor="#ff87ff09"
            android:textSize="30dp"
            android:layout_above="@+id/imageButton"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp" />
    
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:src="@drawable/ic_launcher"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:text="广播意图"
            android:onClick="broadcastIntent"
            android:layout_below="@+id/imageButton"
            android:layout_centerHorizontal="true" />
    
    </RelativeLayout>

    下面是res/values/strings.xml文件的内容,定义了两个新的常量。

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Android Broadcast Receiver</string>
        <string name="action_settings">Settings</string>
        <string name="menu_settings">Settings</string>
        <string name="title_activity_main">Main Activity</string>
    
    </resources>
  • 相关阅读:
    Java--分布式系统高并发解决方案
    Eclipse 快捷键
    Java--基础命名空间
    Java--发送邮件
    Java--垃圾收集算法及内存分配策略
    Java--Vector类
    第四篇 express 安装esasticsearch
    第三篇elasticsearch分布式安装
    第二篇elasticsearch配置
    elasticsearch介绍
  • 原文地址:https://www.cnblogs.com/blog-wangke/p/14454855.html
Copyright © 2011-2022 走看看