`
shenjichao2009
  • 浏览: 97232 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android中Intent的使用

 
阅读更多

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">Intent</string>
 <string name="start">开始</string>
 <string name="stop">停止</string>
</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <Button android:id="@+id/startButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:gravity="center"
  android:text="@string/start" android:layout_margin="10px" />

 <Button android:id="@+id/stopButtonId" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:gravity="center"
  android:text="@string/stop" android:layout_margin="10px" />

</LinearLayout>

 

IntentActivity.java

package com.duoguo.android;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Intent的使用
 *
 * @author shyboy(
897948924@qq.com)
 *
 */

public class IntentActivity extends Activity {

 // 声明Button控件
 private Button startButton;
 private Button stopButton;

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // 根据Button控件的id获取控件对象
  startButton = (Button) findViewById(R.id.startButtonId);
  stopButton = (Button) findViewById(R.id.stopButtonId);

  startButton.setOnClickListener(new StartButtonClickListener());// 为startButton添加单击事件监听器
  stopButton.setOnClickListener(new StopButtonClickListener());// 为stopButton添加单击事件监听器

 }

 // 声明startButton控件的监听器
 class StartButtonClickListener implements OnClickListener {

  @Override
  public void onClick(View v) {

   handler.post(runnable);// 将线程添加到消息队列中

  }

 }

 // 声明stopButton控件的监听器
 class StopButtonClickListener implements OnClickListener {

  @Override
  public void onClick(View v) {

   handler.removeCallbacks(runnable);// 在消息队列中将行将发生的post进行移除

  }

 }

 Handler handler = new Handler();// 实例化Handler对象
 // 实例化线程runnable对象
 Runnable runnable = new Runnable() {

  @Override
  public void run() {

   System.out.println("runnable……");
   handler.postDelayed(runnable, 3000);// 在3秒时间过后将线程添加到消息队列中

  }
 };

}

 

希望能够给你带来帮助

分享到:
评论

相关推荐

    android中intent使用示例

    总结,Intent是Android系统中连接各个组件的桥梁,理解并熟练使用Intent对于开发Android应用至关重要。在实际项目中,Intent不仅可以用于启动Activity和Service,还可以用于启动BroadcastReceiver,实现各种组件间的...

    Android中Intent使用、数据回写(显)

    - 使用`putExtra()`方法可以在Intent中添加额外的数据,如`intent.putExtra("key", "value");` - 数据类型包括基本类型(int、String等)、Parcelable(自定义对象、Bitmap等)和Serializable(复杂对象)。 3. *...

    Android的Intent实验

    - `putExtra()`: 用于添加额外的数据到Intent中,例如字符串、整数、布尔值等,以便在目标组件中通过`getExtras()`获取。 - `setFlags()`: 设置Intent的标志,影响Intent的处理方式,如FLAG_ACTIVITY_NEW_TASK用于...

    Android中intent的使用

    本篇文章将深入探讨Intent的基本概念、类型、构造方法以及如何在Android中有效地使用Intent。 Intent是一种意图声明,表达了应用程序想要执行的操作。在Android系统中,Intent分为显式Intent和隐式Intent两种类型。...

    Android中Intent的Uri使用

    ### Android中Intent的Uri使用详解 #### 概述 在Android开发中,`Intent`是进行组件间通信的重要工具之一,它可以启动一个Activity、BroadcastReceiver或Service,也可以用来向服务发送数据请求。其中,`Intent`的...

    Android利用Intent启动和关闭Activity

    在Android应用程序开发中,Intent是连接各个组件(如Activity、Service等)的关键桥梁,主要用于启动和关闭Activity。Intent不仅能够启动一个新的Activity,还能在Activity之间传递数据,实现应用内部或应用间的交互...

    android.intent.action.TIME_TICK

    在给定的标题"android.intent.action.TIME_TICK"中,涉及的是一个特定的系统广播,当系统时间每分钟改变时,这个广播就会被发送。这个广播事件对于那些需要定时更新或依赖于系统时间的应用程序非常有用。 描述中...

    Android中Intent和ProgressBar的结合使用

    总的来说,Intent和ProgressBar的结合使用是Android开发中常见的场景,尤其在处理耗时操作时,可以提供良好的用户反馈,提升用户体验。通过理解Intent的原理以及ProgressBar的用法,开发者可以更好地控制应用程序的...

    Android 通过Intent使用Bundle传递对象详细介绍

    Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable。 两种序列化方式的区别: 1.在使用内存的时候,Parcelable比Serializable性能高,推荐使用Parcelable类; 2.Serializable在序列化的时候会...

    android用于打开各种文件的intent.pdf

    Android 中使用 Intent 打开各种文件类型 Android 操作系统提供了 Intent 机制,允许应用程序之间进行交互和通信。Intent 是一个异步的消息机制,用于在应用程序之间请求或提供服务。通过使用 Intent,可以实现打开...

    android中Intent传递对象的两种方法(Serializable,Parcelable)

    当我们需要在Intent中传递复杂对象时,有两种主要的方法:Serializable和Parcelable。这两种方式都可以实现对象的序列化,但它们在性能和效率上存在差异。 **Serializable接口** Serializable接口是Java提供的一个...

    Android使用Intent实现Video功能

    本文将详细探讨如何使用Intent来实现Android中的Video功能。 首先,我们要理解Android的MediaStore类。MediaStore是Android系统提供的一个数据库,它包含了设备上的所有媒体文件,包括图片、音频和视频。通过...

    ex07_activity_intent.rar_Intent_android

    "ex07_activity_intent"这个压缩包文件很可能包含了一系列关于Android中Intent使用的教程或示例代码,让我们来深入探讨Intent的概念、类型以及如何在实际应用中使用它。 1. **Intent的概念**: Intent是一个对象,...

    android intent 使用总结

    Android Intent 使用总结 Android Intent 是 Android 组件之间通讯的核心机制,它负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述。Android 则根据 Intent 的描述,找到对应的组件,将 Intent 传递给...

    android 利用intent调用activity 简明精炼的例子

    3. 传递数据:通过`putExtra()`方法可以向Intent中添加键值对,用于传递数据。数据类型可以是基本类型(如String、int)、Parcelable对象或Serializable对象。例如,传递一个字符串: ```java intent.putExtra(...

    Android Studio 实验二:Intent的使用

    使用putExtra()方法可以将数据附加到Intent中,然后在接收端使用getExtra()系列方法获取这些数据: ```java // 发送端 Intent intent = new Intent(this, TargetActivity.class); intent.putExtra("key", ...

    Android通过Intent传递数据

    在Android应用开发中,Intent是一种强大的机制,用于在组件之间传递信息和启动操作。它扮演着应用程序内部通信的重要角色,特别是在活动(Activity)之间。"Android通过Intent传递数据"这一主题,涵盖了Intent的基本...

    Android+Intent机制实例详解.doc

    在Android开发中,Intent机制扮演着至关重要的角色,它是Android应用程序组件间通信的主要桥梁。Intent不仅用于启动活动(Activity)、服务(Service),还可以传递数据和触发广播接收器(Broadcast Receiver)。...

    Android-Intent-数据存取-ContentProvider.doc

    Intent 是一个将要执行的动作的抽象描述,通常作为参数来使用,由 Intent 来协助完成在 Android 组件之间的通讯。 1. Intent 的作用 Intent 主要用来启动其他的 Activity 或者 Service,所以可以将 Intent 理解成 ...

Global site tag (gtag.js) - Google Analytics