安卓手机App开发:
(包括打电话,发短信,音乐播放器,划屏)
环境的搭建
1.软件下载:
Jdk-6u32-windows-x64.exe
Android-sdk_r18-windows.zip
Eclipse-jee-indigo-SR2-win32-x86_64.zip
2环境的搭建概要:
安装Jdk-6u32-windows-x64.exe,根据自己的习惯安装在相应的目录下(Jdk下载点击打开链接)。
完成Eclipse的解压安装。
解压Android-sdk_r18-windows.zip文件,安装android SDK(SDK下载点击打开链接)。
打开Eclipse后选择helpinstall New Software 弹出”Available Software对话框的SDK插件ADT(Eclipse下载点击打开链接)。
创建ADT,完成基本系统安装和配置。
测试开发环境,搭建好开发环境后创建一个Hello world工程。
//MainActivity.java package com.example.activity; import java.io.IOException; import android.app.Activity; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button button; private Button btnPlay; private Button btnStop; private TextView tvName; private int mDownx; private int mDowny; Toast mMoveToast; Toast mDownToast; private AssetManager assetManager;// assets文件夹 private MediaPlayer mPlayer;// 播放音乐类 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { button = (Button) findViewById(R.id.main_id); btnPlay=(Button) findViewById(R.id.btn_play); btnStop=(Button) findViewById(R.id.btn_stop); tvName=(TextView) findViewById(R.id.tv_name); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, MyActivity.class); startActivity(intent); } }); btnPlay.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startPlayer(); } }); btnStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { stopPlayer(); } }); } /* * 启动播放 */ public void startPlayer() { try { if (mPlayer == null) { tvName.setText("aa.mp3"); assetManager = getAssets(); AssetFileDescriptor fileDescriptor = assetManager .openFd("aa.mp3"); mPlayer = new MediaPlayer(); mPlayer.setDataSource(fileDescriptor.getFileDescriptor()); mPlayer.setLooping(true);// 设置重复播放 } if (!mPlayer.isPlaying()) { mPlayer.prepare();// 准备播放 mPlayer.start();// 播放 } } catch (IOException e) { e.printStackTrace(); } } //停止播放 public void stopPlayer(){ if(mPlayer!=null&&mPlayer.isPlaying()){ mPlayer.stop(); } } @Override protected void onPause() { super.onPause(); stopPlayer(); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mDownx = (int) event.getX(); mDowny = (int) event.getY(); if (mDownToast == null) { mDownToast = Toast.makeText(this, "手指按下的坐标:x-->" + mDownx + " y-->" + mDowny, Toast.LENGTH_SHORT); mDownToast.setGravity(Gravity.TOP, 0, 100); } else { mDownToast.setText("手指按下的坐标:x-->" + mDownx + " y-->" + mDowny); } mDownToast.show(); break; case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); int y = (int) event.getY(); int moveX = mDownx - x; int movey = mDowny - y; if (moveX > 20 || movey > 20) { if (mMoveToast == null) { mMoveToast = Toast.makeText(this, "手指滑动的距离:moveX-->" + moveX + " movey-->" + movey, Toast.LENGTH_SHORT); mMoveToast.setGravity(Gravity.CENTER, 0, 0); } else { mMoveToast.setText("手指滑动的距离:moveX-->" + moveX + " movey-->" + movey); } mMoveToast.show(); } break; } return super.onTouchEvent(event); } }
//MyActivity .java package com.example.activity; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; public class MyActivity extends Activity { private EditText mEtPhone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mEtPhone = (EditText) findViewById(R.id.et_phone); // 打电话 findViewById(R.id.btn_call).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phone = mEtPhone.getText().toString(); if (!TextUtils.isEmpty(phone) && phone.length() == 11) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)); startActivity(intent); } } }); //发短信 findViewById(R.id.btn_send_sms).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phone = mEtPhone.getText().toString(); if (!TextUtils.isEmpty(phone) && phone.length() == 11) { Uri uri = Uri.parse("smsto:" + phone); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", ""); startActivity(intent); } } }); } }
//activity_main.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="com.example.activity.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎使用" /> <Button android:id="@+id/main_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="21dp" android:text="点击" /> <Button android:id="@+id/btn_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/main_id" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:text="播放" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/textView1" android:text="暂停" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_play" android:layout_alignLeft="@+id/btn_play" android:layout_alignRight="@+id/btn_stop" android:layout_marginBottom="25dp" android:layout_marginLeft="23dp" android:text="歌曲名称" /> </RelativeLayout>
//activity_my.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="com.example.activity.MyActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="祝您使用愉快" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="14dp" android:hint="电话号码" android:inputType="phone" android:maxLength="11" android:padding="5dp" > <requestFocus /> </EditText> <Button android:id="@+id/btn_send_sms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/btn_call" android:layout_alignRight="@+id/et_phone" android:layout_marginRight="20dp" android:text="发短信" /> <Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_phone" android:layout_below="@+id/et_phone" android:layout_marginTop="21dp" android:text="打电话" /> </RelativeLayout>
//ActivityManifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="20" /> <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".MyActivity" android:label="@string/title_activity_my" > </activity> </application> </manifest></span>