`

Intent的使用

阅读更多

以下内容Sinfrancis版权所有,专注请注明来自  http://mdev.cc/dev

 

在Android中,传递数据使用Intent,Intent相当于各个Activity之间的桥梁,可以传递数据,可以通过Intent启动另外一个Activity。
Intent有显式和隐式之分,显式的是直接什么要启动的组件,比如Service或者Activity,隐式的通过配置的datatype、url、action来找到匹配的组件启动。
此程序目的:
1、显式启动Activity和service
2、通过隐式的变量,启动Activity和Service
 
先来看先我们定义的变量类:

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/";
}
 
程序主界面的代码:还有四个按钮,分别用于启动不同的组件:

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:

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:

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());
 }
}
 ===================================

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 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使用,用于数据操纵使用
分享到:
评论
1 楼 dyllove98 2009-11-30  
很适合新手  谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics