而下面我们要讲的Intent 对象就是为解决这问题而生的,Intent 就如同其英文字义,是"想要"或"意图",之意,在主Activity 当中,告诉程序自己是什么,并想要前往哪里,这就是Intent 对象所处理的事了,本例子和前一个例子我们将实现同一个效果.
首先是布局main.xml 及mylayout.xml
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎来到魏祝林的博客"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout2"
/>
</LinearLayout>
mylayout.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"
android:background="#ffffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击进入Layout1"
/>
</LinearLayout>
然后是控制程序IntentDemo.java 及IntentDemo1.java 代码:
IntentDemo.java:
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo extends Activity {
private Button bt1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//new 一个Intent对象,并指定要启动的Class
Intent intent = new Intent();
intent.setClass(IntentDemo.this, IntentDemo1.class);
//调用一个新的Activity
startActivity(intent);
//关闭原本的Activity
IntentDemo.this.finish();
}
});
}
}
在IntentDemo.java 同一目录内新建一个IntentDemo1.java 类
IntentDemo1.java:
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo1
extends Activity {
private Button bt2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 载入mylayout.xml
setContentView(R.layout.mylayout);
bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// new 一个Intent对象,并指定要启动的Class
Intent intent = new Intent();
intent.setClass(IntentDemo1.this, IntentDemo.class);
// 调用一个新的Activity
startActivity(intent);
// 关闭原本的Activity
IntentDemo1.this.finish();
}
});
}
}
最后是本例子的重点,添加另外一个Activity 所以必须在AndroidManifest.xml 中定义一个新的activty ,并给予名称name ,或则程序无法编译运行.新手很容易遇到这个问题.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.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>
<activity android:name="IntentDemo1"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
本例子所涉及的的全部代码已经全部贴出,最后执行之,将达到上述效果!本节到此结束~
来自网友整理的Intent,当然Android123将会加入更多内容不断完善Intent常见调用帮助网友方便查找。
显示Web网页:
1. Uri uri = Uri.parse("http://www.android123.com.cn");
2. Intent it = new Intent(Intent.ACTION_VIEW,uri);
3. startActivity(it);
显示Google地图:
1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
2. Intent it = new Intent(Intent.Action_VIEW,uri);
3. startActivity(it);
Maps路径规划:
1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
2. Intent it = new Intent(Intent.ACTION_VIEW,URI);
3. startActivity(it);
拨打电话:
1. Uri uri = Uri.parse("tel:xxxxxx");
2. Intent it = new Intent(Intent.ACTION_DIAL, uri);
3. startActivity(it);
1. Uri uri = Uri.parse("tel.xxxxxx");
2. Intent it =new Intent(Intent.ACTION_CALL,uri);
注意需要权限 <uses-permission id="android.permission.CALL_PHONE" />
发送SMS/MMS
1. Intent it = new Intent(Intent.ACTION_VIEW);
2. it.putExtra("sms_body", "android开发网欢迎您");
3. it.setType("vnd.android-dir/mms-sms");
4. startActivity(it);
发送短信
1. Uri uri = Uri.parse("smsto:10086");
2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
3. it.putExtra("sms_body", "10086"); //正文为10086
4. startActivity(it);
发送彩信
1. Uri uri = Uri.parse("content://media/external/images/media/10"); //该Uri根据实际情况修改,external代表外部存储即sdcard
2. Intent it = new Intent(Intent.ACTION_SEND);
3. it.putExtra("sms_body", "android123.com.cn");
4. it.putExtra(Intent.EXTRA_STREAM, uri);
5. it.setType("image/png");
6. startActivity(it);
发送Email
2. Uri uri = Uri.parse("mailto:android123@163.com");
3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
4. startActivity(it);
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_EMAIL, "android123@163.com");
3. it.putExtra(Intent.EXTRA_TEXT, "android开发网测试");
4. it.setType("text/plain");
5. startActivity(Intent.createChooser(it, "选择一个Email客户端"));
1. Intent it=new Intent(Intent.ACTION_SEND);
2. String[] tos={"android123@163.com"}; //发送到
3. String[] ccs={"ophone123@163.com"}; //抄送
4. it.putExtra(Intent.EXTRA_EMAIL, tos);
5. it.putExtra(Intent.EXTRA_CC, ccs);
6. it.putExtra(Intent.EXTRA_TEXT, "正文");
7. it.putExtra(Intent.EXTRA_SUBJECT, "标题");
8. it.setType("message/rfc822"); //编码类型
9. startActivity(Intent.createChooser(it, "选择一个Email客户端"));
Email添加附件
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_SUBJECT, "正文");
3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/nobody.mp3"); //附件为sd卡上的nobody MP3文件
4. sendIntent.setType("audio/mp3");
5. startActivity(Intent.createChooser(it, "选择一个Email客户端"));
播放多媒体
1.
2. Intent it = new Intent(Intent.ACTION_VIEW);
3. Uri uri = Uri.parse("file:///sdcard/nobody.mp3");
4. it.setDataAndType(uri, "audio/mp3");
5. startActivity(it);
1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); //从系统内部的MediaProvider索引中调用播放
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. startActivity(it);
Uninstall卸载程序
1. Uri uri = Uri.fromParts("package", packageName, null); //packageName为包名,比如com.android123.apkInstaller
2. Intent it = new Intent(Intent.ACTION_DELETE, uri);
3. startActivity(it);
Installer安装APK
这里安装APK文件的方法参考 http://www.android123.com.cn/kaifafaq/487.html 一文里面包含两种方法
本文转载自:http://www.android123.com.cn/androidkaifa/612.html