一.打开网页
定义字符串资源文件——strings.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IE</string>
<string name="open_name">标准Action操作</string>
</resources>
</span>
定义布局管理器资源——main.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/MyLayout" >
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开网页" />
</LinearLayout>
</span>
定义Activity程序,操作Intent
<span style="font-size:18px;">package com.example.ie;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button mybut = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mybut = (Button) super.findViewById(R.id.mybut);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://www.baidu.com");
Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setData(uri);
MainActivity.this.startActivity(it);
}
}
}
</span>
显示效果:
二.调用拨号程序
定义布局文件——main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mybut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拨打电话" />
</LinearLayout>
定义Activity程序,调用拨号操作
package com.example.call;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button mybut = null;
private EditText tel = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得组件
this.mybut = (Button) super.findViewById(R.id.mybut);
this.tel = (EditText) super.findViewById(R.id.tel);
this.mybut.setOnClickListener(new OnClickListenerImpl()); //定义单击事件
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String telStr = MainActivity.this.tel.getText().toString();
Uri uri = Uri.parse("tel:" + telStr); //指定数据
Intent it = new Intent(); //实例化Intent
it.setAction(Intent.ACTION_DIAL); //指定Action
it.setData(uri); //设置数据
MainActivity.this.startActivity(it); //启动Activity
}
}
}
在AndroidManifest.xml文件中增加配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.call"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE"/> //配置拨打电话的权限
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity //定义Activity程序
android:name="com.example.call.MainActivity" //程序类名称
android:label="@string/app_name" > //显示标题
<intent-filter> //作为主程序执行
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
显示效果:
输入电话
调用拨号
正在播出电话