BroadcastReceiver会耗费电量吗?

时间:2013-10-20 09:09:52

标签: android

我知道在安装.apk后,AlarmManagerBroadcastReceiver会保留系统以监视短信

即使我从未收到过短信,AlarmManagerBroadcastReceiver也会耗费电量吗?

当我停止观看短信时,是否需要禁用AlarmManagerBroadcastReceiver?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.enabledisablebroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver"
            android:label="@string/title_activity_enable_disable_boradcast_receiver" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Broadcast receiver -->
        <receiver android:name="com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver" >
            <intent-filter>
               <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <service android:name="com.code4reference.enabledisablebroadcastreceiver.MyInternetServer"></service>

    </application>

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

</manifest>





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

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];
            for (int i = 0; i < pduArray.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
                //Log.d("CWCGR1", 
                //      "From: " + messages[i].getOriginatingAddress()+
                //      " Msg: " + messages[i].getMessageBody());
                HandleMsg(context,messages[i].getOriginatingAddress(), messages[i].getMessageBody());
            }
        }       

    }

    private void HandleMsg(Context context,String address, String body ){
        Intent msgIntent = new Intent(context,MyInternetServer.class);        
        msgIntent.putExtra("address", address);
        msgIntent.putExtra("body", body);
        context.startService(msgIntent);
    }
}






import com.example.enabledisablebroadcastreceiver.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class EnableDisableBroadcastReceiver extends Activity {

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

        Button btnExit=(Button)findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }           
        }); 

    }

   public void enableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
   }

   public void disableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
   }   

}

2 个答案:

答案 0 :(得分:7)

正如所讨论的here,传入的SMS属于一组特殊的低级系统事件,这些事件将导致CPU从节省电池的“睡眠模式”中唤醒(此组包括来电,来电移动)数据包和AlarmManager事件)。无论您的广播接收器是否被设置,设备的GSM无线电都将收听来电短信。当SMS到达时,系统将选择哪个接收器将处理它。因此,注册SMS广播接收器对电池消耗没有影响。此外,内存中已注册接收器的能耗为not significant

如果有功能要求,您可以以编程方式取消注册广播接收器。

答案 1 :(得分:2)

因为您已经在Android清单中对AlarmManagerBroadcastReceiver进行了罚款,所以每次收到短信时,android都会唤醒您的应用并调用AlarmManagerBroadcastReceiver。 如果没有收到短信,您的应用将不会被唤醒,因此不会对电池产生任何影响。

如果您想控制何时接收这些广播,请通过上下文中的registerReceiver(BroadcastReceiver接收器,IntentFilter过滤器)方法以编程方式注册和取消注册您的接收器。