阅读更多

--------------------------------------------AndroidManifest.xml----------------------------------

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.ch24"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="10"

        android:targetSdkVersion="15" />

    

    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MainActivity"

            android:label="@string/title_activity_main" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            intent-filter>

        activity>

        

        <receiver android:name=".SimpleSmsBroadcaseReceiver" >

            <intent-filter>

                <action android:name="android.provider.Telephony.SMS_RECEIVED" >

                action>

            intent-filter>

        receiver>

        <receiver android:name=".SmsBroadcaseReceiver" >

            <intent-filter>

                <action android:name="android.provider.Telephony.SMS_RECEIVED" >

                action>

            intent-filter>

        receiver>

        <receiver android:name=".CallInBroadcaseReceiver" >

            <intent-filter>

                <action android:name="android.intent.action.PHONE_STATE" >

                action>

            intent-filter>

        receiver>

    application>

manifest>

--------------------------------------------Layout activity_main.xml----------------------------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/charge"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示当前电量" />

    <Button

        android:id="@+id/registerReceiver"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="已经注册的广播" />

LinearLayout>

--------------------------------------------MainActivity.java--------------------------------------

package com.ch24;

import java.util.List;

import android.app.Activity;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.pm.PackageManager;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

/* 显示当前电量 */

private Button btn_charge;

/* 查看已经动态注册的广播 */

private Button btn_registed;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViews();

setListeners();

}

private void findViews() {

btn_charge = (Button) findViewById(R.id.charge);

btn_registed = (Button) findViewById(R.id.registerReceiver);

}

private void setListeners() {

btn_charge.setOnClickListener(this);

btn_registed.setOnClickListener(this);

}

@Override

public void onClick(View v) {

/* 动态注册广播 */

if (v == btn_charge) {

/* 广播+action* */

registerReceiver(new BatteryBroadcaseReceiver(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

else if (v == btn_registed) {// 查询已经注册

/* 包管理实例 */

PackageManager packageManager = getPackageManager();

Intent intent = new Intent();

intent.setAction("android.intent.action.PHONE_STATE");

List list = packageManager.queryBroadcastReceivers(intent, PackageManager.GET_INTENT_FILTERS);

Log.i("a07""size="+list.size());

}

}

}

--------------------------------------------SimpleSmsBroadcaseReceiver.java------------------

package com.ch24;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

/**

 * 

 * 项目名称:com.ch24    

 * 类名称:SimpleSmsBroadcaseReceiver    

 * 类描述:  自定义广播,接收所有

 * 创建人:方勇   

 * 创建时间:2012-12-13 上午8:45:28   

 * Copyright (c) 方勇-版权所有

 */

public class SimpleSmsBroadcaseReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Log.i("a07""接收所有广播..........");

}

}

--------------------------------------------SmsBroadcaseReceiver.java--------------------------

package com.ch24;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsMessage;

import android.util.Log;

/**

 * 

 * 项目名称:com.ch24    

 * 类名称:SimpleSmsBroadcaseReceiver    

 * 类描述:  自定义广播,接收所有

 * 创建人:方勇   

 * 创建时间:2012-12-13 上午8:45:28   

 * Copyright (c) 方勇-版权所有

 */

public class SmsBroadcaseReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

/* Intent中传递的附加数据 */

Bundle bundle = intent.getExtras();

/* 短信内容 */

Object[] data = (Object[]) bundle.get("pdus");

/* 声明Sms消息数组 */

SmsMessage[] messages = new SmsMessage[data.length];

for (int i = 0; i < messages.length; i++) {

/*设置消息体内容*/

messages[i] = SmsMessage.createFromPdu((byte[]) data[i]);

/* 电话号码 */

String smsnumber = messages[i].getDisplayOriginatingAddress();

/* 短信息内容 */

String smsbody = messages[i].getDisplayMessageBody();

Log.i("a07""smsnumber:"+smsnumber+"|smsbody:"+smsbody);

}

}

}

--------------------------------------------CallInBroadcaseReceiver.java------------------------

package com.ch24;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.util.Log;

/**

 * 

 * 项目名称:com.ch24    

 * 类名称:CallInBroadcaseReceiver    

 * 类描述: 自定义广播,打电话的广播

 * 创建人:方勇   

 * 创建时间:2012-12-13 上午9:11:41   

 * Copyright (c) 方勇-版权所有

 */

public class CallInBroadcaseReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

/* 电话管理对象 */

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);

String incomingTel;

Log.i("a07""来电状态"+telephonyManager.getCallState());

switch (telephonyManager.getCallState()) {

case TelephonyManager.CALL_STATE_RINGING:

incomingTel=intent.getStringExtra("incoming_number");

Log.i("a07""来电号码"+incomingTel);

break;

default:

break;

}

}

}

--------------------------------------------BatteryBroadcaseReceiver.java-----------------------

package com.ch24;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

/**

 * 

 * 项目名称:com.ch24    

 * 类名称:BatteryBroadcaseReceiver    

 * 类描述:电池广播  

 * 创建人:方勇   

 * 创建时间:2012-12-13 上午9:49:10   

 * Copyright (c) 方勇-版权所有

 */

public class BatteryBroadcaseReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

/* 只接收电池改变时的广播 */

if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){

/* 电量 */

int level = intent.getIntExtra("level", 0);

/*刻度*/

int scala = intent.getIntExtra("scala", 100);

Log.i("a07""当前电量为:"+level*100/scala+"%");

}

}

}

--------------------------------------------效果-------------------------------------------------

发短信

打电话

电池电量

  • 大小: 26.4 KB
  • 大小: 12.2 KB
  • 大小: 26.9 KB
  • 大小: 26.7 KB
  • 大小: 2.8 KB
  • com.ch24.rar (692.8 KB)
  • 下载次数: 30
  • 查看图片附件

更多相关文章

  1. Android(安卓)SDK Manager无法更新解决方法
  2. Android(安卓)中的BroadCastReceiver
  3. 最常用的GitHub—— Android(安卓)开源项目整理(精品)
  4. Windows 下 Android(安卓)NDK 环境配置
  5. Android(安卓)拖拽
  6. Broadcast详解
  7. 在eclipse中查看Android(安卓)SDK源代码
  8. JS判断客户端是否是iOS或者Android
  9. Android(安卓)4.0后,自定义Title报错 You cannot combine custom

随机推荐

  1. php isset和empty方法的区别
  2. 如何调试UPDATE函数在PHP中无法正常工作
  3. PHP获取来路域名 关键字
  4. PHP微信公众平台跳转网页实现定位思路 By
  5. PHP更新基于其他表的表数据
  6. 关于php的输出方式
  7. PHP判断客户端是PCweb端还是移动手机端方
  8. AJAX学习之提交表单
  9. Thinkphp5验证类的使用
  10. php 缓存output_buffering和ob_start