您现在的位置是:首页 > 文章详情

Intent的使用

日期:2017-06-05点击:275
在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使用,用于数据操纵使用
专注于企业信息化,最近对股票数据分析较为感兴趣,可免费分享股票个股主力资金实时变化趋势分析工具,股票交流QQ群:457394862

本文转自沧海-重庆博客园博客,原文链接:http://www.cnblogs.com/omygod/archive/2009/12/22/1629958.html,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/356897
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章