标签:
IP拨号器,使用了Android的广播接收者(BroadCastReceiver),在广播中把已保存的ip号码放在拨打电话号码的前面(getResultData()),然后把修改后的号码设置到广播中(setResultData())。
项目创建步骤和思路:
1.创建一个java类,继承android的BroadCastReceiver
2.在清单文件中,定义receiver节点,定义name属性,指定广播接收者java类的全类名
3.在intent-filter的节点中,指定action子节点,action的值必须跟要接受的广播中的action匹配
4.重写自定义的广播接收者的onReceive()方法,当拨打电话的广播时,把ip号码放在拨打电话的号码前
其中使用SharedPreferences保存ip号码
获取到广播中的数据使用方法:getResultData()
把修改后的数据放入广播方法:setResultData(ipNumber+number);
MainActivity代码:
1 import android.app.Activity; 2 import android.content.SharedPreferences; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.EditText; 6 7 public class MainActivity extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 } 14 15 public void click(View v){ 16 EditText et=(EditText) findViewById(R.id.et); 17 18 SharedPreferences sharedPreferences = getSharedPreferences("ip", MODE_PRIVATE); 19 sharedPreferences.edit().putString("ipNumber", et.getText().toString()).commit(); 20 } 21 22 }
CallReceiver代码:
1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4 import android.content.SharedPreferences; 5 6 public class CallReceiver extends BroadcastReceiver { 7 8 @Override 9 public void onReceive(Context context, Intent intent) { 10 String number = getResultData(); 11 SharedPreferences sharedPreferences = context.getSharedPreferences( 12 "ip", Context.MODE_PRIVATE); 13 String ipNumber = sharedPreferences.getString("ipNumber", ""); 14 System.out.println(ipNumber+"+"+number); 15 setResultData(ipNumber+number); 16 17 } 18 19 }
清单文件代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.lijingbo.ipdialer" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="21" /> 10 11 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 12 13 <application 14 android:allowBackup="true" 15 android:icon="@drawable/ic_launcher" 16 android:label="@string/app_name" 17 android:theme="@style/AppTheme" > 18 <activity 19 android:name=".MainActivity" 20 android:label="@string/app_name" > 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN" /> 23 24 <category android:name="android.intent.category.LAUNCHER" /> 25 </intent-filter> 26 </activity> 27 28 <receiver android:name=".CallReceiver" > 29 <intent-filter> 30 <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 31 </intent-filter> 32 </receiver> 33 </application> 34 35 </manifest>
标签:
原文地址:http://www.cnblogs.com/liyiran/p/5066161.html