• intent大致使用


    intent是同一个或不同组件之间传递消息媒介。主要包括action和data:例如,ACTION_DEAL content://contacts/people/1 显示拨号界面,并填充标识为1的人的信息;

    ACTION_VIEW tel:123 显示拨号电话界面,并填充给定的号码(123);其他的次要部分如 category(类别)、type(数据类型)、component(组件)、extras(附加信息)、flag(如何启动目标activity)。

    关键代码如下

    1  public void onCreate(Bundle savedInstanceState) {
    2         super.onCreate(savedInstanceState);
    3         setContentView(R.layout.main);
    4             Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("content://contacts/people/1"));
    5         startActivity(intent);
    6     }
    7     

    其中uri是通用资源标志符,Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示。

    Android平台而言,URI主要分三个部分:scheme、authority 和 path。

     其中authority又分为host和port。格式如下:scheme://host:port/path

    实际的例子:

    上面的代码在模拟器中执行结果如下:,调出第一个联系人的信息。

    另一个例子

    1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    2         intent.setType("vnd.android.cursor.item/phone");
    3         startActivity(intent);

    上面的代码在模拟器中执行结果如下:,调出所有联系人号码。


    BroadcastReceive:接收来自系统和应用的广播。广播的注册分为静态注册和动态注册,静态注册是在AndroidManifest.xml文件中写入如下代码

    1 <receiver android:name="project_name.Project_nameActivity">
    2     <intent-filter>
    3         <action android:name="android.intent.action.Project_nameActivity"/>
    4         <category android:name="android.intent.category.DEFAULT/>
    5     </intent-filter>
    6 </receiver>

    动态注册

    MyBroadcastReceive receive = new MyBroadcastReceive();

    IntentFilter filter = new IntentFilter();

    filter.addAction("android.intent.action.MyBroadcastReceiver");

    registerReceiver(receiver,filter);


      intent 启动另一个Activity(FirstActivity 启动SecondActivity)

     1 import android.app.Activity;
     2 import android.content.Intent;
     3 import android.os.Bundle;
     4 import android.view.View;
     5 import android.widget.Button;
     6 
     7 public class FirstActivity extends Activity {
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.firstactivity_layout);// 设置页面布局
    13         Button button = (Button) findViewById(R.id.button);// 通过ID值获得按钮对象
    14         button.setOnClickListener(new View.OnClickListener() {// 为按钮增加单击事件监听器
    15 
    16             public void onClick(View v) {
    17                 Intent intent = new Intent();// 创建Intent对象
    18                 intent.setAction(Intent.ACTION_VIEW);// 为Intent设置动作
    19                 startActivity(intent);// 将Intent传递给Activity
    20             }
    21         });
    22     }
    23 }
     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 
     4 public class SecondActivity extends Activity {
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.secondactivity_layout);// 设置页面布局
    10     }
    11 }
  • 相关阅读:
    C语言数据类型取值范围
    C语言的概述--学习c的第二天
    开始学习c语言
    Comet 反Ajax: jQuery与PHP实现Ajax长轮询
    jquery的toFixed方法的正确使用
    JavaScript 正则表达式
    JavaScript中原型链存在的问题解析
    vue编程式导航
    vue 动态组件,传递参数
    JavaScript如何封装插件
  • 原文地址:https://www.cnblogs.com/hxjbc/p/5172060.html
Copyright © 2020-2023  润新知