總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月26日 星期四

BroadcastReceiver ~ 自訂IntentFilter監聽電量、開機、簡訊事件








Activity_main.java

package com.tsots.Compare_BroadcastReceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity_main extends Activity 
{
	Button b1;
	TextView tv1, tv2, tv3;
	String from = "There is no BroadcastReceiver yet";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button) findViewById (R.id.button1);

        tv1 = (TextView) findViewById (R.id.textview1);
        tv2 = (TextView) findViewById (R.id.textview2);
        tv3 = (TextView) findViewById (R.id.textview3);

        /*
         * 透過Intent.ACTION_BATTERY_CHANGED取得battery資訊
         */
        b1.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) 
			{
				BroadcastReceiver_Battery br = new BroadcastReceiver_Battery();	
				registerReceiver(br, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
			}        	
        });
    }
 
    /*
     * 過濾從以下class傳來的封包
     * BroadcastReceiver_Battery.java
     * BroadcastReceiver_Boot.java
     * BroadcastReceiver_SMS.java
     */
    @Override
	protected void onResume() 
	{
		super.onResume();
		try
    	{
	    	IntentFilter mFilter1, mFilter2, mFilter3;
	    	mFilter1 = new IntentFilter(BroadcastReceiver_Battery.customize_ACTION);
	    	mFilter2 = new IntentFilter(BroadcastReceiver_Boot.customize_ACTION);
	    	mFilter3 = new IntentFilter(BroadcastReceiver_SMS.customize_ACTION);
	    	BroadcastReceiver_customize BRC = new BroadcastReceiver_customize();
	    	registerReceiver(BRC, mFilter1);
	    	registerReceiver(BRC, mFilter2);
	    	registerReceiver(BRC, mFilter3);
    	}
    	catch(Exception e)
    	{
    		
    	}
	}
	
    /*
     * 透過自訂的BroadcastReceiver擷取從以下class傳來的封包
     * BroadcastReceiver_Battery.java
     * BroadcastReceiver_Boot.java
     * BroadcastReceiver_SMS.java
     * 並取得存在Service的資訊
     */
    public class BroadcastReceiver_customize extends BroadcastReceiver 
    {
    	static final String customize_ACTION = "com.tsots.Compare_BroadcastReceiver.CUSTOMIZE";
    	@Override
    	public void onReceive(Context context, Intent intent) 
    	{
    		if (intent.getAction().equals(customize_ACTION)) 
    	    {
    	        Bundle bundle_from = intent.getExtras();
    	        if (bundle_from != null)
    	        {
    	        	from = bundle_from.getString("FROM");
    	        }
    	    }
    		System.out.println(from);
    		if(from.equals("the BroadcastReceiver is from BroadcastReceiver_Battery"))
    		{
    			tv1.setText(new Service_receiveBR().str_battery);
    		}
    		else if(from.equals("the BroadcastReceiver is from BroadcastReceiver_Boot"))
    		{
    			tv2.setText(new Service_receiveBR().str_boot);
    		}
    		else
    		{
    			tv3.setText(new Service_receiveBR().str_sms);
    		}    		    		    		
    	}
    }
}

BroadcastReceiver_SMS.java

package com.tsots.Compare_BroadcastReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class BroadcastReceiver_SMS extends BroadcastReceiver 
{
	private static final String sms_ACTION = "android.provider.Telephony.SMS_RECEIVED";
	static final String customize_ACTION = "com.tsots.Compare_BroadcastReceiver.CUSTOMIZE";
	@Override
	public void onReceive(Context context, Intent intent) 
	{
	    if (intent.getAction().equals(sms_ACTION)) 
	    { 
	      StringBuilder sb = new StringBuilder(); 
	      Bundle bundle = intent.getExtras(); 
	      if (bundle != null) 
	      { 
	        Object[] myOBJpdus = (Object[]) bundle.get("pdus"); 
	        SmsMessage[] messages = new SmsMessage[myOBJpdus.length];  
	        for (int i = 0; i

BroadcastReceiver_Boot.java
package com.tsots.Compare_BroadcastReceiver;

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

public class BroadcastReceiver_Boot extends BroadcastReceiver 
{
	private static final String boot_ACTION = "android.intent.action.BOOT_COMPLETED";
	static final String customize_ACTION = "com.tsots.Compare_BroadcastReceiver.CUSTOMIZE";
	//取得系統時間
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		if (intent.getAction().equals(boot_ACTION)) 
	    {
	        StringBuilder sb = new StringBuilder(); 
		    sb.append("開機時間: ");   
		    sb.append(sdf.format(new java.util.Date()));
	        Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

	        //啟動Service服務並將boot資訊傳過去
	        intent.setClass(context, Service_receiveBR.class);
		    Bundle bundle_br_boot = new Bundle();
		    bundle_br_boot.putString("MESSAGE_BOOT", sb.toString());
		    intent.putExtras(bundle_br_boot);
		    context.startService(intent);
	        
		    //送出廣播"android.intent.action.BOOT_COMPLETED"
		    Intent intent_BroadcastToActivity = new Intent(customize_ACTION);
		    intent_BroadcastToActivity.putExtra("FROM", "the BroadcastReceiver is from BroadcastReceiver_Boot");
		    context.sendBroadcast(intent_BroadcastToActivity);
	    }
	}
}
BroadcastReceiver_Battery.java
package com.tsots.Compare_BroadcastReceiver;

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

public class BroadcastReceiver_Battery extends BroadcastReceiver 
{
	private static final String battery_ACTION = "android.intent.action.BATTERY_CHANGED";
	public static final String customize_ACTION ="com.tsots.Compare_BroadcastReceiver.CUSTOMIZE";
	private int intLevel;
	private int intScale; 
	
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		if (intent.getAction().equals(battery_ACTION)) 
	    {
			//level: 電池剩餘容量
			intLevel = intent.getIntExtra("level", 0);
			//scale: 電池最大值;通常為100
	        intScale = intent.getIntExtra("scale", 100); 

	        StringBuilder sb = new StringBuilder(); 
		    Bundle bundle = intent.getExtras(); 
		    if (bundle != null) 
		    { 
		    	sb.append("目前電量:");
		        sb.append(String.valueOf(intLevel * 100 / intScale) + "%");   
		    } 
	        Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
	        
	        //啟動Service服務並將battery資訊傳過去
	        intent.setClass(context, Service_receiveBR.class);
		    Bundle bundle_br_battery = new Bundle();
		    bundle_br_battery.putString("MESSAGE_BATTERY", sb.toString());
		    intent.putExtras(bundle_br_battery);
		    context.startService(intent);
		    
		    //送出廣播"android.intent.action.BOOT_COMPLETED"
		    Intent intent_BroadcastToActivity = new Intent(customize_ACTION);
		    intent_BroadcastToActivity.putExtra("FROM", "the BroadcastReceiver is from BroadcastReceiver_Battery");
		    context.sendBroadcast(intent_BroadcastToActivity);
	    }
	}
}
Service_receiveBR.java
package com.tsots.Compare_BroadcastReceiver;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

/*
 * 用此Service存住battery,boot,SMS的資訊
 */
public class Service_receiveBR extends Service
{
	static String str_battery;
	static String str_boot;
	static String str_sms;
	@Override
	public void onCreate() 
	{
		super.onCreate();		
	}
	
	@Override
	public void onStart(Intent intent, int startId) 
	{		
		Bundle bundle = intent.getExtras();
		if(bundle != null)
		{
			str_battery = bundle.getString("MESSAGE_BATTERY");
			str_boot = bundle.getString("MESSAGE_BOOT");
			str_sms = bundle.getString("MESSAGE_SMS");
		}
		super.onStart(intent, startId);		
	}

	@Override
	public IBinder onBind(Intent arg0) 
	{
		// TODO Auto-generated method stub
		return null;
	}	
}

沒有留言:

張貼留言