当前位置: 首页 > news >正文

Intent的使用

在Android中,传递数据使用 IntentIntent相当于各个Activity之间的桥梁,可以传递数据,可以通过 Intent启动另外一个Activity。
Intent有显式和隐式之分,显式的是直接什么要启动的组件,比如Service或者Activity,隐式的通过配置的datatype、url、action来找到匹配的组件启动。
此程序目的:
1、显式启动Activity和service
2、通过隐式的变量,启动Activity和Service
 
先来看先我们定义的变量类:
Java代码
  1. package cc.androidos.intent;   
  2.  public class Book {    
  3.  //Intent的数据类型    
  4.  public static  String CONTENT_TYPE = "cc.android/intent.demo";    
  5.     
  6.  //Intent中的URL,这里要使用Content开头,不然会找不到组件    
  7.  public static String CONTENT_URI = "content://test/";    
  8. }  
package cc.androidos.intent;public class Book { //Intent的数据类型 public static  String CONTENT_TYPE = "cc.android/intent.demo";  //Intent中的URL,这里要使用Content开头,不然会找不到组件 public static String CONTENT_URI = "content://test/";}
 
程序主界面的代码:还有四个按钮,分别用于启动不同的组件:
Java代码 复制代码
  1. package cc.androidos.intent;   
  2.  import android.app.Activity;    
  3.  import android.content.Intent;   
  4.  import android.net.Uri;   
  5.  import android.os.Bundle;   
  6.  import android.view.View;   
  7. import android.widget.Button;   
  8. public class IntentDemo extends Activity {   
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {   
  11.         super.onCreate(savedInstanceState);   
  12.         setContentView(R.layout.main);   
  13.           
  14.           
  15.         Button firstbtn = (Button) findViewById(R.id.firstbtn);   
  16.         Button firstbtnservice = (Button) findViewById(R.id.firstbtnservice);   
  17.         Button secondbtn = (Button) findViewById(R.id.secondbtn);   
  18.         Button secondbtnservice = (Button) findViewById(R.id.secondbtnservice);   
  19.           
  20.           
  21.         firstbtn.setOnClickListener(new View.OnClickListener(){   
  22.    public void onClick(View v) {   
  23.     //显式启动FirstIntentDemo Activity   
  24.     Intent i = new Intent(getApplicationContext(),FirstIntentDemo.class);   
  25.     startActivity(i);   
  26.    }   
  27.         });   
  28.           
  29.         firstbtnservice.setOnClickListener(new View.OnClickListener(){   
  30.    public void onClick(View v) {   
  31.     //显式启动FirstService 后台服务   
  32.     Intent i = new Intent(getApplicationContext(),FirstService.class);   
  33.     startService(i);   
  34.    }   
  35.             
  36.         });   
  37.           
  38.           
  39.         secondbtn.setOnClickListener(new View.OnClickListener(){   
  40.    public void onClick(View v) {   
  41.        
  42.     //通过Action uri和dataype启动Activity   
  43.     //程序会自动匹配到Intent-Filter配置中有(action属性)Action为Intent.ACTION_VIEW,并且数据类型(data)为cc.android/intent.demo的组件上   
  44.     Intent intent = new Intent();   
  45.     intent.setAction(Intent.ACTION_VIEW);   
  46.     intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE);   
  47.     startActivity(intent);   
  48.    }   
  49.         });   
  50.         secondbtnservice.setOnClickListener(new View.OnClickListener(){   
  51.    public void onClick(View v) {   
  52.     //通过Action uri和dataype启动Service   
  53.     Intent intent = new Intent();   
  54.     intent.setAction(Intent.ACTION_EDIT);   
  55.     intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE);   
  56.     startService(intent);   
  57.    }   
  58.         });   
  59.           
  60.     }   
  61. }   
  62.      
  63.    
package cc.androidos.intent;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;public class IntentDemo extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                      Button firstbtn = (Button) findViewById(R.id.firstbtn);        Button firstbtnservice = (Button) findViewById(R.id.firstbtnservice);        Button secondbtn = (Button) findViewById(R.id.secondbtn);        Button secondbtnservice = (Button) findViewById(R.id.secondbtnservice);                      firstbtn.setOnClickListener(new View.OnClickListener(){   public void onClick(View v) {    //显式启动FirstIntentDemo Activity    Intent i = new Intent(getApplicationContext(),FirstIntentDemo.class);    startActivity(i);   }        });               firstbtnservice.setOnClickListener(new View.OnClickListener(){   public void onClick(View v) {    //显式启动FirstService 后台服务    Intent i = new Intent(getApplicationContext(),FirstService.class);    startService(i);   }                 });                      secondbtn.setOnClickListener(new View.OnClickListener(){   public void onClick(View v) {        //通过Action uri和dataype启动Activity    //程序会自动匹配到Intent-Filter配置中有(action属性)Action为Intent.ACTION_VIEW,并且数据类型(data)为cc.android/intent.demo的组件上    Intent intent = new Intent();    intent.setAction(Intent.ACTION_VIEW);    intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE);    startActivity(intent);   }        });        secondbtnservice.setOnClickListener(new View.OnClickListener(){   public void onClick(View v) {    //通过Action uri和dataype启动Service    Intent intent = new Intent();    intent.setAction(Intent.ACTION_EDIT);    intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE);    startService(intent);   }        });           }}   
 
以下分别是被启动的组件代码:
显式Activity和Service:
Java代码 复制代码
  1. package cc.androidos.intent;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. public class FirstIntentDemo extends Activity {   
  5.  @Override  
  6.  protected void onCreate(Bundle savedInstanceState) {   
  7.   super.onCreate(savedInstanceState);   
  8.   setContentView(R.layout.firstintent);   
  9.  }   
  10. }   
  11. ===============================================   
  12. package cc.androidos.intent;   
  13. import android.app.Service;   
  14. import android.content.Intent;   
  15. import android.os.IBinder;   
  16. import android.util.Log;   
  17. public class FirstService extends Service{   
  18.  @Override  
  19.  public IBinder onBind(Intent intent) {   
  20.   return null;   
  21.  }   
  22.     
  23.  @Override  
  24.  public void onCreate() {   
  25.   super.onCreate();   
  26.      
  27.   String tag = "First Service on Create";   
  28.   Log.d(tag,"This is the first service...");   
  29.  }   
  30. }  
package cc.androidos.intent;import android.app.Activity;import android.os.Bundle;public class FirstIntentDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.firstintent); }}===============================================package cc.androidos.intent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class FirstService extends Service{ @Override public IBinder onBind(Intent intent) {  return null; }  @Override public void onCreate() {  super.onCreate();    String tag = "First Service on Create";  Log.d(tag,"This is the first service..."); }}
 隐式启动的Activity和Service:
Java代码 复制代码
  1. package cc.androidos.intent;   
  2. import android.app.Activity;   
  3. import android.content.Intent;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6. public class SecondIntentDemo extends Activity {   
  7.  @Override  
  8.  protected void onCreate(Bundle savedInstanceState) {   
  9.   super.onCreate(savedInstanceState);   
  10.   String tag = "SecondIntentDemo onCreate..";   
  11.   setContentView(R.layout.secondintent);   
  12.   Intent i = getIntent();   
  13.   Log.d(tag, "intent type : " + i.getType());   
  14.   Log.d(tag, "intent url : " + i.getData().toString());   
  15.  }   
  16. }  
package cc.androidos.intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;public class SecondIntentDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  String tag = "SecondIntentDemo onCreate..";  setContentView(R.layout.secondintent);  Intent i = getIntent();  Log.d(tag, "intent type : " + i.getType());  Log.d(tag, "intent url : " + i.getData().toString()); }}
 ===================================
Java代码 复制代码
  1. package cc.androidos.intent;   
  2. import android.app.Service;   
  3. import android.content.Intent;   
  4. import android.os.IBinder;   
  5. import android.util.Log;   
  6. public class SecondService extends Service {   
  7.  @Override  
  8.  public IBinder onBind(Intent arg0) {   
  9.   return null;   
  10.  }   
  11.     
  12.  @Override  
  13.  public void onCreate() {   
  14.   super.onCreate();   
  15.   String tag = "Second service ";   
  16.   Log.d(tag, "Startup second service... ");   
  17.  }   
  18. }   
  19.    
package cc.androidos.intent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class SecondService extends Service { @Override public IBinder onBind(Intent arg0) {  return null; }  @Override public void onCreate() {  super.onCreate();  String tag = "Second service ";  Log.d(tag, "Startup second service... "); }} 
 
AndroidManifest.xml文件配置:
 
这个很重要,需要配置好才行,不然会出现AcitvityNotFoundException
Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cc.androidos.intent"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".IntentDemo"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.           
  15.         <!-- First Intent Demo  -->  
  16.         <activity android:name=".FirstIntentDemo"  android:label="@string/app_name">  
  17.         </activity>  
  18.           
  19.           
  20.         <!-- Second Intent Demo  -->  
  21.           <activity android:name=".SecondIntentDemo"  android:label="@string/app_name">  
  22.          <intent-filter >  
  23.          <!--The intent filter parameters must match the intent datatype(mimeType) \ action  -->  
  24.           <action android:name="android.intent.action.VIEW"/>  
  25.           <data android:mimeType="cc.android/intent.demo"/>  
  26.           <category android:name="android.intent.category.DEFAULT"/>  
  27.          </intent-filter>  
  28.         </activity>  
  29.           
  30.           
  31.         <service android:name=".FirstService" >  
  32.         </service>  
  33.           
  34.          <service android:name=".SecondService" >  
  35.           <intent-filter >  
  36.            <!--The intent filter parameters must match the intent datatype(mimeType) \ action  -->  
  37.            <action android:name="android.intent.action.EDIT"/>  
  38.           <data android:mimeType="cc.android/intent.demo"/>  
  39.           <category android:name="android.intent.category.DEFAULT"/>  
  40.           </intent-filter>  
  41.              
  42.         </service>  
  43.           
  44.           
  45.     </application>  
  46. </manifest>   
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cc.androidos.intent"      android:versionCode="1"      android:versionName="1.0.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".IntentDemo"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>               <!-- First Intent Demo  -->        <activity android:name=".FirstIntentDemo"  android:label="@string/app_name">        </activity>                      <!-- Second Intent Demo  -->          <activity android:name=".SecondIntentDemo"  android:label="@string/app_name">         <intent-filter >         <!--The intent filter parameters must match the intent datatype(mimeType) \ action  -->          <action android:name="android.intent.action.VIEW"/>          <data android:mimeType="cc.android/intent.demo"/>          <category android:name="android.intent.category.DEFAULT"/>         </intent-filter>        </activity>                      <service android:name=".FirstService" >        </service>                <service android:name=".SecondService" >          <intent-filter >           <!--The intent filter parameters must match the intent datatype(mimeType) \ action  -->           <action android:name="android.intent.action.EDIT"/>          <data android:mimeType="cc.android/intent.demo"/>          <category android:name="android.intent.category.DEFAULT"/>          </intent-filter>                  </service>                  </application></manifest> 
  Intent理由个URI属性,这个配合ContentProvider使用,用于数据操纵使用

相关文章:

  • 《设计原本—计算机科学巨匠Frederick P. Brooks的反思》一一2.3 理性模型有哪些长处...
  • 沟通在一个运维服务项目中的重要性
  • 一个APP从启动到主页面显示经历了哪些过程?
  • 超级简单:如何更快的将数据导入Excel
  • springboot同mybatis整合
  • 博达路由器常见功能教学1
  • 阿特斯副总裁邢国强:2017年底光伏组件成本2元以下
  • python实战===使用随机的163账号发送邮件
  • CSS
  • 刚刚接触的涉及html的工具
  • Net设计模式之简单工厂模式(Simple Factory Pattern)
  • MySQL QA
  • 中文编程是否可行之验证——(一)
  • Provisioning Services 7.6 入门到精通系列之九:创建和配置目标设备
  • 基于OHCI的USB主机 —— 中断处理程序
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • Apache Zeppelin在Apache Trafodion上的可视化
  • CSS魔法堂:Absolute Positioning就这个样
  • HTTP--网络协议分层,http历史(二)
  • Invalidate和postInvalidate的区别
  • JSDuck 与 AngularJS 融合技巧
  • Laravel 实践之路: 数据库迁移与数据填充
  • Linux后台研发超实用命令总结
  • Python socket服务器端、客户端传送信息
  • Ruby 2.x 源代码分析:扩展 概述
  • Sass 快速入门教程
  • select2 取值 遍历 设置默认值
  • vue 配置sass、scss全局变量
  • Vue.js-Day01
  • 闭包--闭包之tab栏切换(四)
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 从零开始的无人驾驶 1
  • 搞机器学习要哪些技能
  • 给第三方使用接口的 URL 签名实现
  • 检测对象或数组
  • 简单基于spring的redis配置(单机和集群模式)
  • 设计模式 开闭原则
  • 什么是Javascript函数节流?
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 为什么要用IPython/Jupyter?
  • 06-01 点餐小程序前台界面搭建
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​linux启动进程的方式
  • #NOIP 2014# day.2 T2 寻找道路
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)