Intent的功能有:

在mainActivity中为按钮1添加监听事件:

listener1 = new OnClickListener() {

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       Intent intent1 = new Intent(mainActivity.this, Activity1.class);
        intent1.putExtra("mainActivity", "这是来自mainActivity的数据");
        startActivityForResult(intent1, REQUEST_CODE);
    }
};

在Activity1中接收来自mainActivity中Intent中的数据:

String data = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    data = extras.getString("mainActivity");
}
setTitle("现在在Activity1里:" + data);

为Activity1中的按钮添加监听事件,返回一个Intent:

listener1 = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString("store", "数据来自Activity1");
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        };

在mainActivity中覆写onActivityResult()方法,对返回的内容处理:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                setTitle("取消");
            } else if (resultCode == RESULT_OK) {
                String temp = null;
                Bundle extras = data.getExtras();
                if (extras != null) {
                    temp = extras.getString("store");
                }
                setTitle("在mainActivity中:"+temp);
            }
        }
    }

为按钮2添加监听事件:

protected static final String ACTION1 = "com.sunny.action.BROADCASE";

listener2 = new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(ACTION1);
                sendBroadcast(intent2);
            }
        };

添加一个Broadcast Receiver,其捕获action为com.sunny.action.BROADCASE的Intent,生成Notification:

public class broadcastReceive1 extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 0;
    Context context;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.context=context;
       showNotification();
    }

private void showNotification() {
        // TODO Auto-generated method stub
        NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
        PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
        notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

}

其在AndroidManifest.xml中注册:

<receiver android:name=".broadcastReceive1">
    <intent-filter>
        <action android:name="com.sunny.action.BROADCASE" />
    </intent-filter>
</receiver>

Intent的功能有:

在mainActivity中为按钮1添加监听事件:

listener1 = new OnClickListener() {

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       Intent intent1 = new Intent(mainActivity.this, Activity1.class);
        intent1.putExtra("mainActivity", "这是来自mainActivity的数据");
        startActivityForResult(intent1, REQUEST_CODE);
    }
};

在Activity1中接收来自mainActivity中Intent中的数据:

String data = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    data = extras.getString("mainActivity");
}
setTitle("现在在Activity1里:" + data);

为Activity1中的按钮添加监听事件,返回一个Intent:

listener1 = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString("store", "数据来自Activity1");
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        };

在mainActivity中覆写onActivityResult()方法,对返回的内容处理:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                setTitle("取消");
            } else if (resultCode == RESULT_OK) {
                String temp = null;
                Bundle extras = data.getExtras();
                if (extras != null) {
                    temp = extras.getString("store");
                }
                setTitle("在mainActivity中:"+temp);
            }
        }
    }

为按钮2添加监听事件:

protected static final String ACTION1 = "com.sunny.action.BROADCASE";

listener2 = new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(ACTION1);
                sendBroadcast(intent2);
            }
        };

添加一个Broadcast Receiver,其捕获action为com.sunny.action.BROADCASE的Intent,生成Notification:

public class broadcastReceive1 extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 0;
    Context context;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.context=context;
       showNotification();
    }

private void showNotification() {
        // TODO Auto-generated method stub
        NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
        PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
        notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

}

其在AndroidManifest.xml中注册:

<receiver android:name=".broadcastReceive1">
    <intent-filter>
        <action android:name="com.sunny.action.BROADCASE" />
    </intent-filter>
</receiver>

android 之 Intent、broadcast的更多相关文章

  1. Android中Intent具体解释(二)之使用Intent广播事件及Broadcast Receiver简单介绍

    通过第一篇的解说,我们已经看到了怎样使用Intent来启动新的应用程序组件,可是实际上他们也能够使用sendBroadcast方法来在组件间匿名的广播消息. 作为一个系统级别的消息传递机制,Inten ...

  2. Android随笔之——Android广播机制Broadcast详解

    在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理.这个广播跟我们传统意义中的电台广播有些相似之处.之所以叫做广播,就 ...

  3. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...

  4. Android学习笔记--Broadcast, BroadcastReceiver(广播)

    参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...

  5. Android四大组件--Broadcast Receiver具体解释

    本文主要讲述了: 一.BroadcastReceiver概述: 二.BroadcastReceiver事件分类 三.BroadcastReceiver事件的编程流程 四.两类BroadcastRece ...

  6. Android组件之BroadCast简单实践

    作为Android的四大组件之一,没有理由不介绍一下BroadCast,BroadCast中文简单翻译就是广播,前阵子浙江某大学的啦啦操,广场舞的大妈,其中大妈和学生从喇叭和音响上听到的声音就是事件源 ...

  7. 【转】【Java/Android】Intent的简介以及属性的详解

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  8. Android之Intent和Activity

    Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...

  9. Android学习之Broadcast初体验

    •何为 Broadcast ? Broadcast 直译广播,接下来举个形象的例子来理解下 Broadcast: 上学的时候,每个班级都会有一个挂在墙上的大喇叭,用来广播一些通知,比如,开学要去搬书, ...

  10. Android 笔记 Intent and Bundle day7

    学习了Intent与Bundle的使用,进行应用中的交互 package com.example.intent; import android.app.Activity; import android ...

随机推荐

  1. vue 引用其他组件

    1.在components 目录下新建Test.vue 文件 <template> <div class="test"> <h1>{{ msg ...

  2. javascript for/forEach

    基本用法 for:for(var i=0;i<arr.length;i++) forEach:arr.forEach(function(value,index,arr){},),其中functi ...

  3. 常用的 JavaScript 简写方法

    1.三元操作符 当想写 if...else 语句时,使用三元操作符来代替. const x = 20; let answer; if (x > 10) {    answer = 'is gre ...

  4. Java编程基础-面向对象(下)

    一.抽象类 1.引入:当定义一个类时,常常需要定义一些方法来描述该类的行为特征,但有时这些方法的实现方式是无法确定的.Java允许在定义方法时不写方法体,不包含方法体的方法为抽象方法,抽象方法必须使用 ...

  5. [Luogu1343]地震逃生 最大流

    题目链接:https://www.luogu.org/problem/show?pid=1343 dinic跑最大流. #include<cstdio> #include<cstri ...

  6. java实现打开Windows控制台窗口

    在写Python程序的时候突发奇想了一下,能不能用java代码实现打开控制台窗口呢? 经过查询网络资料和java API文档,终于实现了: package com.primeton.cmd; impo ...

  7. codevs 1606 台阶

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 话说某牛家门外有一台阶,这台阶可能会很高(总层数<=1000000). 这 ...

  8. 更新Svn客户端后,右键菜单中没有TortoiseSVN

    环境: OS:                 Windows XP sp3 升级后SVNServer:    VisualSVN Server 2.7.3 升级后SVNClient:    小乌龟: ...

  9. Resize a UIImage the right way

    When deadlines loom, even skilled and experienced programmers can get a little sloppy. The pressure ...

  10. Mac 安装和卸载 Mysql5.7.11 的方法

    安装 去http://www.mysql.com/downloads/, 选择最下方的MySQL Community Edition,点击MySQL Community Server的download ...