首页 技术 正文
技术 2022年11月6日
0 收藏 615 点赞 1,055 浏览 6006 个字

Intent的功能有:

android 之 Intent、broadcast

在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);
            }
        }
    }

android 之 Intent、broadcastandroid 之 Intent、broadcastandroid 之 Intent、broadcast

为按钮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、broadcastIntent的功能有:

android 之 Intent、broadcast

在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);
            }
        }
    }

android 之 Intent、broadcastandroid 之 Intent、broadcastandroid 之 Intent、broadcast

为按钮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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896