利用广播实现ip拨号
1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存" />
</LinearLayout>
2.Activity
public class MainActivity extends Activity {
private EditText et_ip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_ip = (EditText) this.findViewById(R.id.et_ip);
SharedPreferences sf = this
.getSharedPreferences("config", MODE_PRIVATE);
this.et_ip.setText(sf.getString("ip", ""));
}
public void save(View view) {
String ip = this.et_ip.getText().toString().trim();
SharedPreferences sf = this
.getSharedPreferences("config", MODE_PRIVATE);
Editor edit = sf.edit();
edit.putString("ip", ip);
edit.commit();
Toast.makeText(this, "保存完毕!", 0).show();
}
}
3.自定义一个广播
public class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "电话通了。。。。", 0).show();
System.out.println("电话通了。。。。");
String number = getResultData();
SharedPreferences sf = arg0.getSharedPreferences("config",
Context.MODE_PRIVATE);
String ip = sf.getString("ip", "");
String newNumber = ip + number;
setResultData(newNumber);
}
}
4.在清单文件中配置
<receiver android:name=".OutCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" >
</action>
</intent-filter>
</receiver>
短信接收广播
1.自定义一个广播类
public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "接收到新短信!", 1).show();
}
}
2.清单文件
<activity
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name="com.itheima.smsreciver.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=".SmsReceiver">
<intent-filter >
<action android:name=" android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
短信窃听器
1.自定义广播类
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
String body = message.getMessageBody();
String sender = message.getOriginatingAddress();
System.out.println("body = " + body);
System.out.println("sender = " + sender);
if ("5556".equals(sender)) {
// 终止广播
abortBroadcast();
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(sender, null, "i hite you", null, null);
}
}
}
}
2.授权
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<receiver android:name=".SmsListener">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
自定义广播时间&发送自定义广播&广播接受者优先级
一.自定义广播接收者应用
1.自定义三个广播接收者
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"自定义广播接收到广播1" , 0).show();
}
}
public class MyBroadcastReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"自定义广播接收到广播2" , 0).show();
}
}
public class MyBroadcastReceiver3 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"自定义广播接收到广播3" , 0).show();
abortBroadcast();
}
}
2.清单文件注册广播接收者<receiver android:name=".MyBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="com.itheima.xxxooo"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiver2">
<intent-filter android:priority="-1000">
<action android:name="com.itheima.xxxooo"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiver3">
<intent-filter android:priority="1">
<action android:name="com.itheima.xxxooo"/>
</intent-filter>
</receiver>
二.广播发送者应用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
Intent intent = new Intent();
intent.setAction("com.itheima.xxxooo");
// 把这个自定义广播发送出去
// 发送一条无序的广播事件
// 如果广播事件是无序发送出去的,所有的广播接收者都会接收到这个事件
// sendBroadcast(intent);
// 如果广播事件是有序发送出去的,广播接收者会按照优先级接收到这个事件
// sendOrderedBroadcast(intent, null);
// Your own BroadcastReceiver to treat as the final receiver of the
// broadcast.
sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 0, null,
null);
}
}
class FinalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是finalReceiver", 0).show();
}
}