首页 > 代码库 > android手机拨号器实现
android手机拨号器实现
总共分为4个部分:string值,Layout布局设计,MainActivity代码编写,给项目添加使用授权。
1. string值
Phone-->res-->values-->strings.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">手机拨号器</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="reminder">请输入手机号</string> <string name="call">拨打此号码</string> </resources>
2. layout布局设计
1. activity_main.xml及fragment_main.xml的处理
新建的android application project默认有2个xml:activity_main.xml及fragment_main.xml,刚开始在fragment_main.xml里添加的Button,但添加好Button代码后应用老是崩溃,因此后来在网上找到了一种删除fragment_main.xml的方法:
1. 删除Phone-->res-->layout-->fragment_main.xml文件;
2. 删除Phone-->res-->layout-->activity_main.xml文件里的内容,添加如下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > </LinearLayout>
3.修改Phone-->src-->com.example.phone-->MainActivity.java代码,注释掉下面部分代码:
package com.example.phone; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 注释掉 if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } */ } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ /* 注释掉 public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } */ }
2. layout布局代码, Phone-->res-->layout-->activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/reminder" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:id="@+id/mobile" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/call" android:id="@+id/button" /> </LinearLayout>
3. MainActivity代码编写
Phone-->src-->com.example.phone-->MainActivity.java:
package com.example.phone; ... // 系统默认import的类省略不写 import android.widget.Button; import android.app.Activity; import android.widget.EditText; import android.content.Intent; import android.net.Uri; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); if (savedInstanceState == null) { } } private final class ButtonClickListener implements View.OnClickListener{ public void onClick(View v){ EditText mobileText = (EditText) findViewById(R.id.mobile); String number = mobileText.getText().toString(); Uri uri = Uri.parse("tel:" + number); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); } } // 其余系统生成代码省略未显示 ... }
4. 给项目添加权限
intent.ACTION_DIAL需要用到CALL_PHONE权限,添加方法如下:
在Phone-->AndroidManifest.xml里添加如下代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.phone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.phone.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> <permission android:name="android.permission.CALL_PHONE" /> </manifest>
倒数第二行为新增加的代码。
其它:
android源码下载:
方法一: 去https://github.com/android/网站,查找到想要的项目,然后点击右边的"Download ZIP"按钮;
方法二:方法一里的代码并不完全,比如没有app/Dialer的代码,此时可以去https://android.googlesource.com/查找,然后用git Clone(当然要先安装git);
方法三:直接在网上搜索别人打包好的整套android源码(我在百度网盘里搜索到的...)。
效果: