广播接收者-IP拨号器
广播接收者——四大组件之一 在android系统里面有很多内置的广播事件 1. 电池电量低. 2. 短信到来了 3. 电话拨打出去了 4. sd卡被插进来了. 5. 屏幕锁屏了. 6. 手机重启了. |
广播接收者编写步骤: 1、写一个类继承BroadCastReceiver import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* 买一个收音机
*
*/
public class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("只要有电话外拨 ,就会调用这个方法");
String number = getResultData();//获取到外拨的电话号码
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
String ipnumber = sp.getString("ipnumber", "");
setResultData(ipnumber+number);
}
}2、在AndroidManifest.xml中进行注册 <receiver android:name="com.itheima.ipdail.OutCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
</receiver>
|
核心代码: MainActivity.java import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText et_number;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
sp = getSharedPreferences("config", MODE_PRIVATE);
et_number.setText(sp.getString("ipnumber", ""));
}
public void click(View view){
String ipnumber = et_number.getText().toString().trim();
Editor editor = sp.edit();
editor.putString("ipnumber", ipnumber);
editor.commit();
}
} OutCallReceiver.java import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* 买一个收音机
*
*/
public class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("只要有电话外拨 ,就会调用这个方法");
String number = getResultData();//获取到外拨的电话号码
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
String ipnumber = sp.getString("ipnumber", "");
setResultData(ipnumber+number);
}
}
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.ipdail"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.ipdail.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>
<receiver android:name="com.itheima.ipdail.OutCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
</receiver>
</application>
</manifest>
|