四大組件:
Activity
Content provider 內容提供者
Broadcast receiver 廣播接受者
Service 服務
電臺: 發送廣播
收音機: 接受廣播
android系統下的廣播:
電池電量低。
電池充電完畢
短信到來了
程序安裝卸載
sd卡卸載 安裝
1.寫一個類繼承廣播接受者
2.在清單文件配置關心的動作
3.一旦廣播事件發生了,就會執行廣播接受者的onreceive方法
MainActivity:
package com.itheima.ipdail;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
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);
//設置一個名字爲config權限爲MODE_PRIVATE的緩存
sp = getSharedPreferences("config", MODE_PRIVATE);
}
public void save(View view){
String ipnumber = et_number.getText().toString().trim();
if(TextUtils.isEmpty(ipnumber)){
Toast.makeText(this, "清除ip號碼成功", 0).show();
}else{
Toast.makeText(this, "設置ip號碼成功", 0).show();
}
Editor editor = sp.edit();
editor.putString("ipnumber", ipnumber);
editor.commit();
}
}
OutCallReceiver:
package com.itheima.ipdail;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* 1.創建一個收音機 繼承廣播接受者
*
*/
public class OutCallReceiver extends BroadcastReceiver {
//當接收到消息對應的方法
@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();
System.out.println("哈哈,有電話打出去了"+number);
//獲得上下文的SharedPreferences
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
//獲取名爲ipnumber的ip前綴字符串
String ipnumber = sp.getString("ipnumber", "");
//判斷是否是長途。是否有前綴
setResultData(ipnumber+number);
}
}
清單文件:
<?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"/>
</intent-filter>
</receiver>
</application>
</manifest>
短信廣播接收者
/**
* 1.創建一個收音機 繼承廣播接受者
*
*/
public class OutCallReceiver extends BroadcastReceiver {
//當接收到消息對應的方法
@Override
public void onReceive(Context context, Intent intent) {
//獲得廣播的內容number
String number = getResultData();
if("5556".equals(number)){
setResultData(null);
}
}
}
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("短信到來了。 。。。");
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objs) {
// 得到短信對象
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
String body = smsMessage.getMessageBody();
String sender = smsMessage.getOriginatingAddress();
System.out.println("body:" + body);
System.out.println("sender:" + sender);
// 終止掉當前的廣播。
if ("5556".equals(sender)) {
abortBroadcast();
}
}
}
}
申明服務與權限:
設置接收優先級priority-1000~1000的優先範圍
<receiver android:name="com.itheima.smsreceiver.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!-- 配置廣播接受者 -->
<receiver android:name="com.itheima.smsreceiver.OutCallReceiver">
<intent-filter android:priority="1000">
<!-- 配置廣播接收者關心的事件是外撥電話 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
自己自定義發送廣播
/**
* 發送廣播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定義一個廣播動作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一聲 把消息發出去了。 無序廣播
sendBroadcast(intent);
//發送有序廣播
//sendOrderedBroadcast(intent, receiverPermission);
}
接收廣播的BroadcastReceiver:
<receiver android:name="com.itheima.police.PoliceReceiver">
<intent-filter >
<action android:name="com.itheima.sender.jiuminga"/>
</intent-filter>
</receiver>