我们知道Intent跳转功能十分强大,可以通过设置属性可以从当前程序跳转到系统程序及其他程序,下面以几个常用的跳转为例做一个简单的实现。
布局文件如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.briup.intentexample.MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浏览器搜索“天气”"
android:onClick="google" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地图"
android:onClick="map" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="路径规划"
android:onClick="round" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发短信"
android:onClick="send" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送短信"
android:onClick="sendto" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="录音机"
android:onClick="sendc" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="详细信息"
android:onClick="detail" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="联系人"
android:onClick="open" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="联系人"
android:onClick="open2" />
</LinearLayout>
Activity类文件如下
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void google(View v){//使用浏览器,不限Google
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "天气");
startActivity(intent);
}
public void map(View v){//打开地图软件并定位指定坐标
Uri uri = Uri.parse("geo:38.88995533,-77.0366576");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
public void round(View v){//路径规划
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
public void send(View v){//发送短息
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "SMS");//信息内容
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
public void sendto(View v){//直接发送短信
Uri uri = Uri.parse("smsto:18862000000");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", "SMS");
startActivity(intent);
}
public void sendc(View v){//打开录音机
Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(intent);
}
public void detail(View v){//显示应用详细信息
Uri uri = Uri.parse("market://details?id=com.briup.intentexample");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
public void open(View v){//SIM卡联系人
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("vnd.android.cursor.item/phone");
startActivityForResult(intent, RESULT_CANCELED);
}
public void open2(View v){//本地联系人
Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK,uri);
startActivityForResult(intent, RESULT_CANCELED);
}
}
效果如下,不做具体测试