您的位置:首页 > 移动开发 > Android开发

Android电话拨号器的实现

2016-02-12 16:14 609 查看
[1]创建广播接受者

[2]在Manifest中进行配置

<?xml version="1.0" encoding="utf-8"?>
<manifest package="jacky.ipdail"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".OutGoingCallReceivce">
<intent-filter>
<!-- 配置广播接收者 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
</receiver>
</application>
<!-- 添加权限 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>

</manifest>


[3]使用SP来保存配置信息,完成数据处理

//MainActivity

package jacky.ipdail;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText et_number;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
}

public void save(View v){
String number = et_number.getText().toString().trim();
//使用SP保存配置信息
SharedPreferences sp = getSharedPreferences("config", 1);
sp.edit().putString("number",number).commit();
Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_LONG).show();
}
}


//OutGoingCallReceivce

package jacky.ipdail;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

/**
* 作者:Jacky
* 邮箱:550997728@qq.com
* 时间:2016/2/12 15:22
*/
public class OutGoingCallReceivce extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

SharedPreferences sp = context.getSharedPreferences("config",1);
String number = sp.getString("number", "0");
//拿到拨打的号码
String currentNumber = getResultData();
//判断是否为长途
if (currentNumber.startsWith("0")) {
//修改拨打的号码
setResultData(number+currentNumber);
}

}
}






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 应用