Activity不能像调用普通Java类一样直接调用,
而要通过Intent对象和startActivity方法调用Activity,
其中Intent对象用于指定要调用哪个Activity。
下面是一个完整的例子,复制就能运行,主要是调用拨号程序和音频设备
效果
界面
<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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="调用系统的activity"
android:textSize="30dp"
android:gravity="center"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn_systemactivity"
android:text="调用拨号程序"
android:textSize="20dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn_systemactivity2"
android:text="调用拨号程序,传入电话号码"
android:textSize="20dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn_systemactivity3"
android:text="调用拨号程序,传入电话号码,拨打电话"
android:textSize="20dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn_systemactivity4"
android:text="启动处理音频的程序"
android:textSize="20dp"
/>
</LinearLayout>
java代码
package com.by.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
/**
*调用系统的activity
*Activity不能像调用普通Java类一样直接调用,
*而要通过Intent对象和startActivity方法调用Activity,
*其中Intent对象用于指定要调用哪个Activity。
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_systemactivity(View v){
// 调用拨号程序
Intent intent=new Intent("com.android.phone.action.TOUCH_DIALER");
startActivity(intent);
}
public void btn_systemactivity2(View v){
// 调用拨号程序
Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:15388639869"));
startActivity(intent);
}
public void btn_systemactivity3(View v){
// 调用拨号程序
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:15388639869"));
startActivity(intent);
}
public void btn_systemactivity4(View v){
// 调用音频设备
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivity(Intent.createChooser(intent,"选择播放设备"));
}
}
在主清单文件中开启打电话权限
<uses-permission android:name="android.permission.CALL_PHONE"/>