2

我知道安装 .apk 后,AlarmManagerBroadcastReceiver 将留在系统中以监视 SMS

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

停止观看 SMS 时是否需要禁用 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();
   }   

}
4

2 回答 2

8

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

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

于 2013-10-23T00:23:53.983 回答
2

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

如果您想控制何时接收这些广播,请通过 Context(例如 Activity)上的 registerReceiver(BroadcastReceiver receiver, IntentFilter filter) 方法以编程方式注册和注销您的接收器。

于 2013-10-23T00:18:49.213 回答