主面板布局:layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnStartSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start SecondActivity" />
<Button
android:id="@+id/btnBrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浏览网页" />
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨打电话" />
<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动拨号面板" />
<Button
android:id="@+id/btnUninstall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卸载应用程序" />
<Button
android:id="@+id/btnInstall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="安装应用程序" />
<Button
android:id="@+id/btnSendSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送短信" />
<Button
android:id="@+id/btnPlayMusic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放音乐" />
</LinearLayout>
主面板调用java代码:
package com.sxt.day04_06;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
}
private void setListener() {
findViewById(R.id.btnBrowser).setOnClickListener(this);
findViewById(R.id.btnCall).setOnClickListener(this);
findViewById(R.id.btnDial).setOnClickListener(this);
findViewById(R.id.btnInstall).setOnClickListener(this);
findViewById(R.id.btnPlayMusic).setOnClickListener(this);
findViewById(R.id.btnSendSms).setOnClickListener(this);
findViewById(R.id.btnStartSecondActivity).setOnClickListener(this);
findViewById(R.id.btnUninstall).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.btnBrowser://浏览网页
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
break;
case R.id.btnCall://打电话
intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:15555215554"));
break;
case R.id.btnDial://启动拨号面板
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:68337799"));
break;
case R.id.btnInstall: {//找到sdk中的安装文件,然后进行安装
// 找到sd卡的Download目录
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(dir, "baidu_safe.apk");
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
}
break;
case R.id.btnPlayMusic://播放音乐文件
intent = new Intent(Intent.ACTION_VIEW);
File dir = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(dir, "yielaixiang.mp3");
intent.setDataAndType(Uri.fromFile(file), "audio/mp3");
break;
case R.id.btnSendSms://发送短信
intent=new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:13377558899"));
intent.putExtra("sms_body", "hello android!");
break;
case R.id.btnStartSecondActivity://隐示意图跳转到另一个activity
intent=new Intent("com.sxt.day04_06.SecondActivity");
break;
case R.id.btnUninstall://卸载安装好的文件
intent=new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:com.sxt.day04_01"));
break;
}
startActivity(intent);
}
}
次面板布局:layout/activity_second.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
次面板java代码:
package com.sxt.day04_06;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i("main","SecondActivity.onCreate()");
}
}
清单xml:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxt.day04_06"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE"/> (这两个是权限的)
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sxt.day04_06.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>
<activity
android:name="com.sxt.day04_06.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="com.sxt.day04_06.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
效果: