Android开发之Intent、广播和接收
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中的额外信息。
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:path = "contacts"
android:scheme="content"/>
</intent-filter>
接下来就是写子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"
>
<TextView
android:id="@+id/itemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:textSize="16px"
android:textColor="#FFF"
/>
</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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/selected_contact_textView"
/>
<Button
android:id="@+id/pick_content_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick Contact"
/>
</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"
>
<ListView
android:id="@+id/contactListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blueeagle"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContactPicker"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:path = "contacts"
android:scheme="content"/>
</intent-filter>
</activity>
<activity android:name=".ContactPickerTest"
android:label="Cotent Pick Tester">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</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的数据。
<intent-filter>
<action android:name="com.todd.NEW_BROADCAST" />
</intent-filter>
</receiver>
这样就把"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>