ITeye博客

Android开发之Intent、广播和接收

googgoogle · 2012-01-31 13:28 · java · Android开发之Intent、广播和接收 · Markdown

       intent.setClass(WelcomeView.this, OAMainView.class);

       startActivity(intent);       

2. 隐式的Intent和延时的运行时绑定。

       startActivity(intent);

Android会解析这个Intent,并启动一个新的活动,该活动会提供对这个电话号码进行拨号的动作。

       Intent intent = new Intent();

       intent.setClass(WelcomeView.this, OAMainView.class);

       startActivityForResult(intent,SUB_ACTIVITY);  

这个和常规的activity一样,这种有码的子activity也可以被显示或者隐式的调用。

          startActivityForResult(intent,SUB_ACTIVITY);

当子Activity可以关闭的时候,则在调用finish之前调用setResult,并向调用它的父Activity返回一个结果。

android:label=”test”>

<intent-filter>

<action

android:name=”com.blueeagle.intent.action.MY_TEST”>

</action>

<category android:name=”android.intent.category.DEFAULT”/>

<category

android:name=”android.intent.category.ALTERNATIVE_SELECTED”

/>

<data android:mimeType=”vnd.android.cursor.dir/*”/>

</intent-filter>

</activity>

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Intent intent = getIntent();

使用getData和getAction方法来查找Intent的数据和动作。使用类型安全的get<type>Extra方法来提取存储在它的附件Bundle中的额外信息。

            &lt;action android:name="android.intent.action.PICK" /&gt;

            &lt;category android:name="android.intent.category.DEFAULT" /&gt;

            &lt;data android:path = "contacts"

                android:scheme="content"/&gt;

        &lt;/intent-filter&gt;

接下来就是写子Activity了。创建一个子Activity,这里就是为什么时候是隐式的原因:(我的理解是这样)

import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts.People; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;/*

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

&gt;

<TextView

android:id="@+id/itemTextView"  

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:padding="10px"

android:textSize="16px"

android:textColor="#FFF"

/&gt;

</LinearLayout>

Contentpicktester.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"

&gt;

<TextView

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:id="@+id/selected_contact_textView"

/&gt;

&lt;Button

android:id="@+id/pick_content_button"  

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:text="Pick Contact"

/&gt;

</LinearLayout>

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"

&gt;

<ListView

android:id="@+id/contactListView"

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

/&gt;

</LinearLayout> AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  package="com.blueeagle"

  android:versionCode="1"

  android:versionName="1.0"&gt;

&lt;uses-sdk android:minSdkVersion="8" /&gt;



&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;

        &lt;activity android:name=".ContactPicker"

              android:label="@string/app_name"&gt;

        &lt;intent-filter&gt;

            &lt;action android:name="android.intent.action.DIAL" /&gt;

            &lt;category android:name="android.intent.category.DEFAULT" /&gt;

            &lt;data android:path = "contacts"

                android:scheme="content"/&gt;

        &lt;/intent-filter&gt;

    &lt;/activity&gt;

    &lt;activity android:name=".ContactPickerTest"

              android:label="Cotent Pick Tester"&gt;

        &lt;intent-filter&gt;

            &lt;action android:name="android.intent.action.MAIN" /&gt;

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;

        &lt;/intent-filter&gt;

    &lt;/activity&gt;

&lt;/application&gt;

&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt;

</manifest>

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;

public static final String NEW_BROADCAST = "com.todd.NEW_BROADCAST";  

/* 这个静态常量字符串可以自己随便定义,代表自定义的action*/

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Button mybutton = (Button)findViewById(R.id.mybutton);

    mybutton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {

        Intent myIntent = new Intent(NEW_BROADCAST);

        myIntent.putExtra("MSG", "地瓜地瓜,我是土豆!");

        myIntent.setAction(NEW_BROADCAST);

        sendBroadcast(myIntent);

        }

        });

}

}main.xml

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast;

/** Called when the activity is first created. */

@Override

public void onReceive(Context context, Intent intent) {

   // TODO Auto-generated method stub

   String message = intent.getExtras().getString("MSG");

   Toast.makeText(context, message, Toast.LENGTH_LONG).show();

   System.out.println(message);

}

} 到这里还不算完。需要有一个注册的过程。注册的过程,就是让接收工程知道我要接收来自哪一个Intent的数据。

        &lt;intent-filter&gt;

            &lt;action android:name="com.todd.NEW_BROADCAST" /&gt;

        &lt;/intent-filter&gt;

    &lt;/receiver&gt;

这样就把"com.todd.NEW_BROADCAST"动作注册到了接收工程中。如此,接收工程完毕。

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

}

}当然,在相应的AndroidManifest.xml文件中要修改,写为: <activity android:name=".IntentRes" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。