#Intent与组件通信具体步骤
##效果图
##音乐存放路径
##代码实现
MainActivity.java
package com.qsd;import java.io.File;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 com.qsd.ch5_4.R;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn_click1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);Uri data = Uri.parse(\"http://www.baidu.com\");intent.setData(data);startActivity(intent);// Intent intent = new Intent(Intent.ACTION_VIEW);// intent.setData(Uri.parse(\"http://www.baidu.com\"));// startActivity(intent);// Uri uri = Uri.parse(\"http://www.baidu.com\");// Intent intent = new Intent(Intent.ACTION_VIEW,uri);// startActivity(intent);}});findViewById(R.id.btn_click2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_DIAL);intent.setData(Uri.parse(\"tel:15555215556\"));startActivity(intent);}});findViewById(R.id.btn_click3).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//Intent intent = new Intent(Intent.ACTION_VIEW);//intent.setType(\"vnd.android-dir/mms-sms\");// \"sms_body\"为固定内容//intent.putExtra(\"sms_body\", \"具体短信内容\");//startActivity(intent);Intent intent = new Intent(Intent.ACTION_SENDTO);intent.setData(Uri.parse(\"smsto:18780260012\"));// //\"sms_body\"为固定内容intent.putExtra(\"sms_body\", \"具体短信内容\");startActivity(intent);}});findViewById(R.id.btn_click4).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);// 获得文件路径,后边是虚拟机存储卡的路径//Uri data = Uri.fromFile(new File(\"/mnt/sdcard/libai.mp3\"));Uri data = Uri.fromFile(new File(\"/mnt/sdcard/tysg.mp3\"));// 设置data+type属性// 方法:Intent android.content.Intent.setDataAndType(Uri data,// String type)intent.setDataAndType(data, \"audio/*\");startActivity(intent);}});findViewById(R.id.btn_click5).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_DELETE);Uri data = Uri.parse(\"package:com.qsd.ch5_4\");intent.setData(data);startActivity(intent);}});findViewById(R.id.btn_click6).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_VIEW);// 路径不能写成:\"file:///storage/sdcard0/···\"File file = new File(\"/storage/sdcard/ch5_4.apk\");Uri data = Uri.fromFile(file);// Type的字符串为固定内容intent.setDataAndType(data,\"application/vnd.android.package-archive\");startActivity(intent);}});}}
activity_main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"android:layout_width=\"match_parent\"android:layout_height=\"match_parent\"android:orientation=\"vertical\" ><Buttonandroid:id=\"@+id/btn_click1\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click1\" /><Buttonandroid:id=\"@+id/btn_click2\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click2\" /><Buttonandroid:id=\"@+id/btn_click3\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click3\" /><Buttonandroid:id=\"@+id/btn_click4\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click4\" /><Buttonandroid:id=\"@+id/btn_click5\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click5\" /><Buttonandroid:id=\"@+id/btn_click6\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:layout_margin=\"20dp\"android:gravity=\"center\"android:padding=\"8dp\"android:text=\"@string/click6\" /></LinearLayout>
##注意:
音乐导入
window>>show view>>File explorer